#P8343. Minimum Weight Submatrix

    ID: 21522 Type: Default 1000ms 256MiB

Minimum Weight Submatrix

Minimum Weight Submatrix

Given a matrix of dimensions r×sr \times s where each 1×11 \times 1 cell has a unique cost, you are to determine a non-empty submatrix (i.e. a contiguous rectangular block) that minimizes its weight. For a submatrix with a total price sum mm, its weight is defined as

ma+mb,|m - a| + |m - b|,

where aa and bb are given integers. Output the minimum weight possible among all submatrices.

inputFormat

The first line of the input contains four integers rr, ss, aa, and bb (1r,s3001 \le r, s \le 300, 109a,b109-10^9 \le a, b \le 10^9). Each of the next rr lines contains ss space-separated integers, representing the price of each 1×11 \times 1 cell in the matrix. All prices are distinct and lie within the range [109,109][-10^9, 10^9].

outputFormat

Output a single integer—the minimum weight, as defined by $$|m - a| + |m - b|,$$ where mm is the sum of the prices in the chosen submatrix.

sample

1 1 3 5
10
12

</p>