Covering the grid
Time limit1sMemory limit256 MB
Place corner-touching rectangles chaining from the top-left cell to the bottom-right cell to maximize the sum of covered cell values.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Prefix sum
- Solved
- No attempts yet
Problem
Myungwoo has an by grid with one integer written in each cell. He also has rectangular boards of many sizes, and he covers the grid with several of them under these rules.
- Every board lies inside the grid and parallel to it. Covering only part of a cell is not allowed.
- The first board contains the cell in row 1, column 1.
- From the second board on, each board touches the bottom right corner point of the board placed just before it, and the edges of the two boards must not touch. So if the previous board ends at the cell in row , column , the next board starts at the cell in row , column .
- The last board contains the cell in row , column .
The picture below shows one covering of an 8 by 8 grid that follows the rules.

The score is the sum of the integers written in the covered cells. A cell that no board covers adds nothing. Given the grid, find the largest score Myungwoo can get.
Input
The first line has two natural numbers and .
Each of the next lines has the values of one row, separated by spaces. The absolute value of every entry is at most .
The bounds are .
Output
Print, on one line, the largest score obtainable under the rules.