#167Hard
Maximum sum submatrix
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You are given an integer matrix. Find a rectangular submatrix (at least one element) with the maximum sum of elements. Print this sum.
Input format
The first line contains and (). Each of the next lines contains integers ().
Output format
A single number — the maximum sum.
Examples
Input
4 4 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2
Output
15
Input
1 1 -5
Output
-5
Note
Checking all rectangles with 2D prefix sums is . Faster: choose a top row and a bottom row ; the column sums over ( each with column prefix sums) form a one-dimensional array. Find its maximum subarray with Kadane's algorithm in . In total .
If all elements are negative, the answer is the largest element. In the first example the answer is : from to .
Source: acmp.ru, problem 115, UVa 108.
