View Code of Problem 182

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>

const int inf = (1<<31)-1;
const int MAXN = 1e2+10;

using namespace std;

int a[MAXN][MAXN];

int main()
{
    int n;
    while(~scanf("%d",&n)){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&a[i][j]);
                a[i][j] += a[i][j-1];
            }
        }
        int tmp,mmax=-inf;
        for(int i=0;i<n;i++){
            for(int j=i+1;j<=n;j++){
                tmp = 0;
                for(int k=1;k<=n;k++){
                    tmp += a[k][j]-a[k][i];
                    mmax = max(tmp,mmax);
                    if(tmp<0)tmp = 0;
                }
            }
        }
        cout<<mmax<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}

Double click to view unformatted code.


Back to problem 182