#P7311. [COCI 2018/2019 #2] Maja

[COCI 2018/2019 #2] Maja

Description

The bee Maja is pollinating flowers in a magical meadow. The meadow can be represented as an N×MN \times M matrix. In row ii and column jj, there are Ci,jC_{i,j} unpollinated flowers.

Maja starts from the beehive located at row AA and column BB, travels to some areas of the meadow, and then returns. In 11 step, Maja can move from the current area to an adjacent area (the area to the left, right, above, or below), but it will not leave the meadow. Each time Maja passes through an area, it pollinates all currently unpollinated flowers in that area. But the meadow is magical: after Maja leaves area (i,j)(i,j), all pollinated flowers there disappear, and then Ci,jC_{i,j} unpollinated flowers immediately grow back.

Since Maja cannot fly forever, it will get tired after KK steps. What is the maximum number of flowers Maja can pollinate while starting from the hive and returning to it within KK steps?

Input Format

The first line contains positive integers N,M,A,B,KN, M, A, B, K.

The next NN lines each contain MM integers, where the value for area (i,j)(i,j) is the number of flowers Ci,jC_{i,j}.

No flowers grow in the area where the beehive is located.

Output Format

Output the maximum number of flowers Maja can pollinate while starting from the hive and returning to it within KK steps.

2 2 1 1 2
0 1
2 10
2
2 2 1 1 4
0 5
5 10
20
3 3 2 2 6
5 1 0
1 0 3
1 3 3
15

Hint

Sample 1 Explanation

Maja starts at (1,1)(1,1), first flies downward to pollinate 22 flowers, and then returns.

Sample 2 Explanation

Maja starts at (1,1)(1,1) and flies right, down, up, and left in order. Since Maja passes through (1,2)(1,2) twice, each time it passes, it can pollinate 55 flowers.

Constraints

For 40%40\% of the testdata, K104K \le 10^4.

For 100%100\% of the testdata, 2N,M1002 \le N, M \le 100, 1AN1 \le A \le N, 1BM1 \le B \le M, 2K1092 \le K \le 10^9, 0Ci,j1090 \le C_{i,j} \le 10^9, and Kmod2=0K \bmod 2 = 0.

Notes

The score of this problem follows the original COCI setting, with a full score of 110110.

This problem is translated from COCI2018-2019 CONTEST #2 T4 Maja.

Translated by ChatGPT 5