fork(1) download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  7. int n, m; cin >> n >> m;
  8. vector<vector<int> > a(n, vector<int>(m));
  9. for (int i = 0; i < n; i++) {
  10. for (int j = 0; j < m; j++) {
  11. cin >> a[i][j];
  12. if (j > 0) a[i][j] += a[i][j - 1]; // prefix sum for i-th row
  13. }
  14. }
  15. int ans = 0;
  16. for (int i = 0; i < m; i++) { // fix i
  17. for (int j = i + 1; j < m; j++) { // fix j
  18. int sum = 0;
  19. for (int k = 0; k < n; k++) { // iterate from first row
  20. sum += a[k][j];
  21. if (i > 0) sum -= a[k][i - 1];
  22. ans = max(ans, sum);
  23. if (sum < 0) sum = 0;
  24. }
  25. }
  26. }
  27. cout << ans;
  28. return 0;
  29. }
Success #stdin #stdout 0s 5660KB
stdin
4 4
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
stdout
15