Omar Loves Candies
Time limit3sMemory limit128 MB
Find the largest sum of any non-empty sub-rectangle in a grid whose rows and columns strictly increase.
- Level
Medium6 of 10
- Topics
- Prefix sum, Greedy, Matrix
- Solved
- No attempts yet
Problem
Omar loves eating a lot of candy, but unfortunately most candy is not healthy. His parents therefore gave every candy a score. A higher score means a healthier candy, and a score is an integer that can be positive, zero or negative.
One day Omar went shopping for candy with his parents and they found a strange store. The store lays its candy out in a grid of rows with candies in each row. Rows are numbered to from top to bottom, columns are numbered to from left to right, and every cell holds one candy.
The display follows a rule. Every candy outside the first row has a higher score than the candy directly above it, and every candy outside the first column has a higher score than the candy directly to its left.
There is only one way to buy candy here: pick a sub-rectangle of the grid and buy every candy inside it. A sub-rectangle is the set of cells shared by a block of consecutive rows through and a block of consecutive columns through (, ). A group of candies with any other shape cannot be picked.
Omar's parents want the non-empty sub-rectangle whose candy scores add up to the largest possible total. Find that total.
Input
The first line contains one integer , the number of test cases ().
Each test case begins with a line holding two integers separated by a single space, and (), the dimensions of the candy grid. The next lines each contain the scores of one row, separated by single spaces. The grid satisfies the rule above, and every score is an integer between and inclusive.
Output
For each test case, print one line with the largest score sum reachable from a non-empty sub-rectangle.