Moving 5
Time limit1sMemory limit512 MB
Find the maximum candy sum along a monotone path from (1,1) to (N,M) in an N by M grid where room (i,j) holds A_i*10^9 + B_j.
- Level
Medium6 of 10
- Topics
- Greedy, Math, Dynamic programming
- Solved
- No attempts yet
Problem
Jungyu is trapped in an N×M maze. The maze is divided into 1×1 rooms, and each room contains some candy. The number of candies in room (i, j) is . The top-left room of the maze is (1, 1), and the bottom-right room is (N, M).
Jungyu is currently at (1, 1) and wants to move to (N, M). When Jungyu is at (r, c), he can move to (r+1, c) or (r, c+1), and each time he visits a room, he can take all the candies in it. He cannot leave the maze.
Find the maximum number of candies Jungyu can take when he moves from (1, 1) to (N, M).
Input
The first line gives the maze dimensions N and M. (1 ≤ N, M ≤ 100,000)
The second line gives , and the third line gives . (0 ≤ ≤ 9)
Output
Print the maximum number of candies Jungyu can take when moving to (N, M) on the first line.
Hint
The maze from the sample looks as follows, and for convenience the number of candies in (i, j) is written as .