a1,1 a1,2 ... a1, n
a2,1 a2,2 ... a2, n
::
an, 1 an, 2 ... an, n
Then, create a program that outputs the maximum value of the sum of one or more consecutive terms (submatrix) in the vertical and horizontal directions and ends.
Input
The input data is given in the following format.
n
a1,1 a1,2 ... a1, n
a2,1 a2,2 ... a2, n
::
an, 1 an, 2 ... an, n
n is 1 or more and 100 or less, and ai, j is -10000 or more and 10000 or less.
Output
Print the maximum value on one line.
Examples
Input
3
1 -2 3
-4 5 6
7 8 -9
Output
16
Input
4
1 3 -9 2
2 7 -1 5
-8 3 2 -1
5 0 -3 1
Output
15
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are asked: "Matrix of given integers ... Then, create a program that outputs the maximum value of the sum of one or more consecutive terms (submatrix) in the vertical and horizontal directions and ends." The phrasing is a bit unclear but typical problem: "maximum sum submatrix" (i.e., find the submatrix (contiguous rows and columns) with maximum sum). The examples confirm: first example matrix:
1 -2 3
-4 5 6
7 8 -9
Maximum sum submatrix? Let's compute. Possibly submatrix from row 2-3 and columns 2-3:
5 6
8 -9 -> sum = 5+6+8-9 = 10? Actually 5+6=11, 8-9=-1 => 10. Not 16.
Maybe row 1-3 col 3 only: 3,6,-9 sum=0? No.
Maybe they mean maximum sum of contiguous elements in either a row (horizontal) or column (vertical) and also "ends"? Wait: "the sum of one or more consecutive terms (submatrix) in the vertical and horizontal directions and ends." Could be: maximum sum of a subarray that is either a horizontal segment (consecutive terms in a row) or a vertical segment (consecutive terms in a column). But why "submatrix"? Also "ends" might refer to endpoints? Alternatively, it could be the "maximum sum sub-rectangle" problem.
Let's test: For first example, if we consider all possible submatrices, what is max sum? Let's compute manually.
Matrix:
[ [1, -2, 3],
[-4, 5, 6],
[7, 8, -9] ]
We can compute by brute mental. Submatrix possibilities:
Single elements: max? 8 maybe? Actually values: 1, -2, 3, -4, 5, 6, 7, 8, -9 => max 8.
Two adjacent horizontally: row1: 1+(-2)=-1, (-2)+3=1; row2: -4+5=1, 5+6=11; row3: 7+8=15, 8+(-9)=-1 => max 15.
Two adjacent vertically: col1:1+(-4)=-3, -4+7=3; col2:-2+5=3,5+8=13; col3:3+6=9,6+(-9)=-3 => max 13.
3x3: full sum: 1-2+3-4+5+6+7+8-9 = let's compute: 1-2=-1+3=2-4=-2+5=3+6=9+7=16+8=24-9=15. So max among these is 16 (from 2x2 bottom-left? Actually bottom-left? Wait bottom-left 2x2 from rows2-3, cols1-2: -4+5+7+8 = 16, correct). So output 16 matches maximum sum submatrix.
Second example:
4
1 3 -9 2
2 7 -1 5
-8 3 2 -1
5 0 -3 1
Let's compute brute? Possibly max sum submatrix sum = 15 as output. Let's verify: Maybe submatrix rows2-3 cols2-3? Row2 col2-3:7,-1 sum=6; row3 col2-3:3,2 sum=5; total=11? Not 15. Or rows2-4 cols1-2? Row2:2+7=9; row3:-8+3=-5; row4:5+0=5; total=9-5+5=9? Not 15. Or maybe the submatrix rows1-2 cols2-4? Row1:3-9+2=-4; Row2:7-1+5=11; total=7? Actually -4+11=7. Or rows2-3 cols3-4? Row2:-1+5=4; Row3:2-1=1; total=5. Or rows2-4 cols2-4? Row2:7-1+5=11; Row3:3+2-1=4; Row4:0-3+1=-2; total=13. Or rows1-3 cols1-2? Row1:1+3=4; Row2:2+7=9; Row3:-8+3=-5; total=8. Or rows2-2 col2-4: sum=7-1+5=11. Or rows1-2 col2-2: single 3? Not 15. Let's try a 2x3: rows2-3 cols1-3: Row2:2+7-1=8; Row3:-8+3+2=-3; total=5. Not. rows2-4 cols1-3: Row2:8; Row3:-3; Row4:5+0-3=2; total=7? Actually 8-3+2=7. rows1-2 cols1-4 sum: row1:1+3-9+2=-3; row2:2+7-1+5=13; total=10. rows1-3 cols1-4: add row3:-8+3+2-1=-4 => total=6. rows1-4 cols1-4: add row4:5+0-3+1=3 => total=9? Actually row1-3 sum = -3+13-4=6? Wait recalc: row1 sum= -3? 1+3-9+2 = -3? Actually 1+3=4, 4-9=-5, -5+2=-3. row2:2+7-1+5=13. row3:-8+3+2-1=-4. Sum row1-3 = -3+13-4=6. row4:5+0-3+1=3. Total all=9. So not 15.
Maybe the maximum is 15 from submatrix rows2-2 col2-4? That's 11. Or rows1-3 col3-4? Row1:-9+2=-7; row2:-1+5=4; row3:2-1=1; total=-2. No. rows2-4 col2-3? Row2:7-1=6; row3:3+2=5; row4:0-3=-3; total=8.
Maybe I'm missing a submatrix with sum 15. Let's compute more systematically. Possibly submatrix rows1-4 cols2-3? row1:3-9=-6; row2:7-1=6; row3:3+2=5; row4:0-3=-3; sum=2. Not. rows1-2 cols3-4? row1:-9+2=-7; row2:-1+5=4; sum=-3.
Perhaps the maximum submatrix sum is 15 from a different selection: rows1-2 cols2-4? row1:3-9+2=-4; row2:7-1+5=11; total=7. No.
What about rows1-3 cols2-4? row1:-4? Actually row1 col2-4: 3-9+2=-4; row2:7-1+5=11; row3:3+2-1=4; sum=11. Not.
What about rows2-4 cols1-3? row2:8, row3:-3, row4:2; sum=7.
Maybe submatrix rows2-4 cols2-4 gave 13, but maybe rows1-4 cols1-2 gave 13 as well. But output is 15, so there is a submatrix sum of 15. Let's try to find systematically with algorithm.
Maybe the submatrix is rows2-3 cols1-2 and rows4 col4? No, contiguous rectangle.
Maybe there is a submatrix of size 3x3: rows2-4 cols2-4? row2:11, row3:4, row4:-2 => total=13. rows1-3 cols1-3? we computed 0. rows1-3 cols2-4? row1:-4, row2:11, row3:4 => total=11. rows2-4 cols1-3? row2:8, row3:-3, row4:2 => 7. rows1-3 cols1-2? row1:4, row2:9, row3:-5 => 8.
Maybe a 1x4 horizontal segment: row2: 2+7-1+5=13; row3: -8+3+2-1=-4; row4:5+0-3+1=3; row1: -3. So max horizontal segment is row2 sum=13. Could vertical segment: column 2: 3+7+3+0=13; column 3: -9-1+2-3=-11; column1:1+2-8+5=0; column4:2+5-1+1=7. So max vertical is 13.
But we haven't seen 15. So maybe the max submatrix sum is indeed 13? But output says 15. So I must have miscalculated something.
I'll brute think. Possibly submatrix consisting of rows2 and 3, columns2 and 3: elements: (2,2)=7, (2,3)=-1, (3,2)=3, (3,3)=2 => sum=7-1+3+2 = 11. (I had 11 earlier). Rows2-3, cols1-2: 2,7,-8,3 => sum=2+7-8+3=4. Rows2-3, cols1-3: 2+7-1-8+3+2 = (2+7-1)=8, (-8+3+2)=-3 => total=5? Actually compute: 2+7-1=8; -8+3+2=-3; sum=5. Rows2-3, cols2-4: 7-1+5 +3+2-1 = (7-1+5=11) + (3+2-1=4) =15! Wait that's rows2-3 and columns2-4:
Row2 columns2-4: 7, -1, 5 => sum = 7-1+5 = 11.
Row3 columns2-4: 3, 2, -1 => sum = 3+2-1 = 4.
Total = 11+4 = 15.
Yes! So submatrix rows2-3, cols2-4 sums to 15. That matches output.
So indeed the problem is to find maximum sum submatrix (contiguous rows and columns). So we must implement algorithm for maximum sum rectangle in 2D matrix.
Given constraints n <= 100, values between -10000 and 10000. So O(n^3) algorithm (Kadane's over columns) is feasible: O(n^3) for n=100 => 1e6 operations? Actually O(n^3) = 1e6 (100^3 = 1,000,000). That's fine. We'll implement: for each pair of rows (top, bottom), compute an array of column sums from top to bottom for each column (cumulative). Then run Kadane's algorithm on that 1D array to find max subarray sum for that vertical strip. Keep global max.
We need to consider that submatrix must have at least one element; output should be the maximum sum; if all numbers negative, then we must pick the maximum single element (since one or more consecutive terms; submatrix of size 1 is allowed). So Kadane's should be able to handle negative values: we can either modify Kadane's to track maximum subarray sum, resetting when current sum < 0? Actually standard Kadane's for all-negative arrays returns the maximum element if we handle correctly: initialize max_so_far = first element, current_max = first element, then for each subsequent element: current_max = max(element, current_max + element), and max_so_far = max(max_so_far, current_max). This yields the maximum subarray sum even if all negative. So we can use that.
Alternatively, we could compute column sums using prefix sums for speed.
Pseudo:
Read n, matrix a[n][n].
Precompute prefix sum matrix prefix[i][j] where prefix[i][j] = sum of a[1..i][j]? Actually easier: for each pair of rows top, bottom, we can compute an array colSum[col] = sum_{row=top to bottom} a[row][col]. We can compute that quickly by using row-wise prefix sums per column, but n is small so we can compute directly O(n^2) per pair: O(n^3) total which is 1e6, fine.
Algorithm:
max_sum = -inf (maybe -10^9)
for top in range(n):
temp = [0]*n # array to accumulate column sums for rows from top to bottom
for bottom in range(top, n):
# add row bottom to temp
for col in range(n):
temp[col] += a[bottom][col]
# now run Kadane on temp[0..n-1]
current_max = temp[0]
max_here = temp[0]
for i in range(1, n):
current_max = max(temp[i], current_max + temp[i])
max_here = max(max_here, current_max)
# also need to consider when i starts from 0? Actually we already set max_here = temp[0]; then iterate from 1
# but what if n=1? We handle separately: loop only if n>1, but we can just compute using same logic.
max_sum = max(max_sum, max_here)
Top=0:
bottom=0: temp = [1,-2,3]. Kadane: max subarray? Consider: possible subarrays: 1 (1), -2, 3, 1-2=-1, -2+3=1, 1-2+3=2 => max=3? Actually max subarray is 3 (just element 3) or maybe 1-2+3=2, so max=3. But we might get larger when top=1 bottom=2: temp = [-4+5+6? Actually top=1 bottom=1: row1? Wait careful: top=0, bottom=0 -> row0. top=0 bottom=1 -> rows0-1: [1-4? Actually sum: col0:1+(-4)=-3, col1:-2+5=3, col2:3+6=9 => temp=[-3,3,9]. Kadane: best subarray? 3+9=12 maybe? Or just 9? 3+9=12 is best. So max_here=12. Then bottom=2: add row2: col0: -3+7=4, col1:3+8=11, col2:9+(-9)=0 => temp=[4,11,0]. Kadane: best = 4+11+0=15? Actually subarray 4+11+0=15, or 11+0=11, or just 11? But 15 is max. So max_sum becomes 15 at this point. But actual answer is 16. Wait we haven't gotten 16 yet. Let's continue with other top values.
Top=2:
bottom=2: temp = [7,8,-9]. Kadane: best? 7+8=15? Actually 7+8=15, 8-9=-1, 7+8-9=6 => max=15. So final max=16.
Works.
Example2: n=4, we'll trust.
Thus solution is O(n^3).
We need to implement in Python? The problem statement didn't specify language, but it's likely to be generic. We'll provide Python code.
Potential issues: Input format: first line n, then n lines each with n integers. Output the max sum.
Edge Cases: Negative numbers. Also, n=1: just print that number.
Implementation details: Use sys.stdin.read to read all ints quickly.
We'll produce code:
import sys
def max_subarray(arr):
max_ending_here = max_so_far = arr[0]
for x in arr[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [[0]*n for _ in range(n)]
for i in range(n):
for j in range(n):
a[i][j] = int(next(it))
max_sum = -10**9
for top in range(n):
temp = [0]*n
for bottom in range(top, n):
for col in range(n):
temp[col] += a[bottom][col]
# run Kadane
# but careful: if n==0? not possible
current = temp[0]
best = temp[0]
for i in range(1, n):
current = max(temp[i], current + temp[i])
if current > best:
best = current
if best > max_sum:
max_sum = best
print(max_sum)
if name == "main":
main()
That should work.
But note: The problem statement: "Matrix of given integers ... Then, create a program that outputs the maximum value of the sum of one or more consecutive terms (submatrix) in the vertical and horizontal directions and ends." It could be interpreted as maximum sum of a submatrix that is formed by taking consecutive rows and columns. The phrase "and ends" might be translation artifact. So we are on track.
Hence answer is to implement maximum sum submatrix.
Now write final answer.
But the user says: "Matrix of given integers ... Then, create a program that outputs the maximum value of the sum of one or more consecutive terms (submatrix) in the vertical and horizontal directions and ends." They want the solution.
We must output the maximum value.
Thus we provide explanation and code.
Given the context (maybe they want a solution in C? But they didn't specify language; typical programming problem. We'll produce Python solution.
However, note: The instruction for the assistant: "You are an expert in competitive programming." So we should provide a complete solution.
I'll produce final answer with explanation and code.
Now produce final message. The problem asks to find the maximum sum of a contiguous submatrix (i.e., a rectangular block formed by consecutive rows and columns) in an n × n matrix, with 1 ≤ n ≤ 100 and entries between -10000 and 10000.
The solution uses the classic O(n³) method:
For each pair of top and bottom rows, compute the sum of each column from top to bottom (using an accumulator array).
For each such column-sum array, apply Kadane's 1D maximum subarray algorithm to find the best horizontal segment.
Keep the global maximum over all row intervals.
Kadane's algorithm works even when all numbers are negative, correctly returning the maximum single element.
Below is a Python implementation that reads the matrix and prints the answer.
python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [[0] * n for _ in range(n)]
for i in range(n):
for j in range(n):
a[i][j] = int(next(it))
max_sum = -10**9 # sufficiently small
for top in range(n):
col_sum = [0] * n # accumulates sums for columns
for bottom in range(top, n):
# add row 'bottom' to col_sum
for c in range(n):
col_sum[c] += a[bottom][c]
# Kadane's algorithm on col_sum
cur = best = col_sum[0]
for x in col_sum[1:]:
cur = max(x, cur + x)
best = max(best, cur)
if best > max_sum:
max_sum = best
print(max_sum)
if __name__ == "__main__":
main()