Moving 2
InterviewTime limit2sMemory limit512 MB
Given an N by N grid of candy amounts, collect the most candy over K monotone paths from (1,1) to (N,N), counting each cell only once.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Implementation, Matrix
- Solved
- No attempts yet
Problem
Jungyu is trapped in a maze of size . The maze is divided into rooms of size , and every room holds some candy. A room is written , which means row and column . The top left room is and the bottom right room is .
Jungyu starts at and travels to a total of times. From he can move only to or , and he can never leave the maze. When he enters a room he takes every candy still in it. A room he has already emptied gives him nothing on a later visit. He may take the same route more than once.
Find the largest number of candies he can collect once the trips are done.
Input
The first line contains the maze size and the number of trips . (, )
Each of the next lines contains numbers. The -th number on the -th line is the amount of candy in room , which is at least 0 and at most 1,000.
Output
Print on the first line the largest number of candies Jungyu can collect over the trips. If is 0 he makes no trip, so print 0.