-
Notifications
You must be signed in to change notification settings - Fork 1
/
108 - Maximum Sum .cpp
46 lines (38 loc) · 938 Bytes
/
108 - Maximum Sum .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*Saurav Paul*/
#include<bits/stdc++.h>
using namespace std;
void solve(int n) {
vector<vector<int> > v(n+1,vector<int>(n+1)) ,ud ;
ud = v ;
for(int i = 1 ; i <= n; i++){
for(int j = 1; j <= n ; j++){
cin >> v[i][j] ;
}
}
for(int i = 1 ; i <= n; i++){
for(int j = 1; j <= n; j++){
ud[j][i] = ud[j-1][i] + v[i][j] ;
}
}
long long mx = -INT_MAX ;
for(int r1 = 1 ; r1 <= n; r1 ++){
for(int r2 = r1 ; r2 <= n ; r2 ++){
long long temp = 0 ;
for(int c = 1 ; c <= n ; c++){
temp += ud[r2][c] - ud[r1-1][c] ;
if(temp > mx) mx = temp ;
if(temp < 0) temp = 0 ;
}
}
}
cout << mx << endl ;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n ;
while(cin >> n){
solve(n) ;
}
return 0;
}