You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
#include <bits/stdc++.h>
|
|
|
|
|
using namespace std;
|
|
|
|
|
const int N = 130;
|
|
|
|
|
int a[N][N], s[N][N];
|
|
|
|
|
int main() {
|
|
|
|
|
int n, res = INT_MIN;
|
|
|
|
|
cin >> n;
|
|
|
|
|
for (int i = 1; i <= n; i++)
|
|
|
|
|
for (int j = 1; j <= n; j++) {
|
|
|
|
|
cin >> a[i][j];
|
|
|
|
|
s[i][j] = s[i - 1][j] + a[i][j]; // 利用一维前缀和,把当前第j列前面的1~j-1列的值,累加和压缩到第j列,每列都是如此处理
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// O(N^3)算法
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
for (int j = 0; j < i; j++) { // 前缀和需要下标从0开始
|
|
|
|
|
int sum = 0;
|
|
|
|
|
for (int k = 1; k <= n; k++) {
|
|
|
|
|
sum = max(sum, 0) + s[i][k] - s[j][k];
|
|
|
|
|
res = max(res, sum);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << res << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|