COCI '18 Contest 2 #4 Maja

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Maja the Bee pollinates flowers in a magical meadow. The meadow can be represented as a matrix of N rows and M columns. In i^{th} row and j^{th} column there are C_{i,j} unpollinated flowers.

Maja will start her journey from her hive, which is located in the field in the A^{th} row and B^{th} column. In several steps, she will visit some fields of the meadow and then return back to her hive. From each field, Maja can move to one of its adjacent cells in one of the following directions: left, right, up or down. Also, Maja will never leave the meadow. Each time Maja flies over some field, she pollinates all unpollinated flowers growing on the field. But the meadow is magical! As soon as Maja leaves the field (i, j), all the pollinated flowers will disappear and C_{i,j} new unpollinated flowers will grow on that field.

Since Maja can't fly forever, she will get tired after K steps and gladly tell her adventurous story to her bee friends. What is the maximal number of flowers Maja can pollinate if she makes exactly K steps and ends her journey back at her hive?


The first line contains positive integers N, M (2 \le N, M \le 100), A (1 \le A \le N), B (1 \le B \le M) and K (2 \le K \le 10^9). K will always be even.

N lines follows, each containing M integers describing amount of flowers C_{i,j} (0 \le C_{i,j} \le 10^9) located in i^{th} row and j^{th} column.

The field containing the hive won't have any flowers on it.


Print the number from the task statement.


In test cases worth 40% of the total points it will hold K \le 10\,000.

Sample Input 1

2 2 1 1 2
0 1
2 10

Sample Output 1


Explanation for Sample Output 1

In the first sample Maja starts from the field (1,1), flyes to the field below, pollinates 2 flowers there, and returns back to the hive.

Sample Input 2

2 2 1 1 4
0 5
5 10

Sample Output 2


Explanation for Sample Output 2

In the second sample Maja starts from the field (1,1) and can pollinate flowers moving as follows: she moves right, then down, then up and then left. Notice that Maja visited the field (1,2) twice, each time pollinating 5 flowers on that field.

Sample Input 3

3 3 2 2 6
5 1 0
1 0 3
1 3 3

Sample Output 3



There are no comments at the moment.