Maja
Time limit2sMemory limit512 MB
Maja makes exactly K moves on a grid, starting and ending at the hive, to maximize flowers pollinated, where each cell regrows its flowers after she leaves.
- Level
Medium6 of 10
- Topics
- Matrix, Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Maja the Bee pollinates flowers in a magical meadow. The meadow is an by matrix, and the cell in row and column has unpollinated flowers.
Maja starts from her hive at row , column . She makes several moves, visits some cells of the meadow, and returns to the hive. From each cell she can move to an adjacent cell: left, right, up, or down. She never leaves the meadow. Each time she flies over a cell, she pollinates all unpollinated flowers on it. The meadow is magical, though. As soon as Maja leaves cell , every pollinated flower there disappears, and new unpollinated flowers grow on that cell.
Maja cannot fly forever. After steps she gets tired and happily tells her adventure to her bee friends. What is the maximum number of flowers Maja can pollinate if she makes exactly steps and ends back at the hive?
Input
The first line contains positive integers , , , , and (, , , ). is always even. The next lines each contain integers, the flower counts (). The hive cell has no flowers.
Output
Print the number described in the problem statement.
Hint
In the first sample, Maja starts at , flies to the cell below, pollinates 2 flowers there, and returns to the hive. In the second sample, Maja starts at and moves right, down, up, then left. She visits twice, and each visit pollinates 5 flowers there.