# Maximum Sum

Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. A sub-rectangle is any contiguous sub-array of size 1 × 1 or greater located within the whole array.
As an example, the maximal sub-rectangle of the array:
 0 −2 −7 0 9 2 −6 2 −4 1 −4 1 −1 8 0 −2
is in the lower-left-hand corner and has the sum of 15.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

int s[110][110];
int main()
{
//freopen("in.txt", "r", stdin);
int n;
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
scanf("%d", &s[i][j]);
if(j>=1)
s[i][j] += s[i][j - 1];
}
int ans = -10000000;

for (int i = 0; i < n; i++)
for (int j = i; j < n; j++)
for (int k = 0, temp = 0; k < n;k++)
{
temp += s[k][j] - s[k][i - 1];
ans = (ans > temp) ? ans : temp;
if (temp < 0)temp = 0;
}
printf("%d\n", ans);
}
}

07-11 791

11-15 1105

04-02 756

09-18 2584

09-26 166

07-25 1048

10-03 725

05-21 4688

08-14 3036

02-12 739