-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path108 - Maximum Sum.cpp
55 lines (46 loc) · 1.07 KB
/
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
47
48
49
50
51
52
53
54
55
/// O (n^3) solution
/// Kadan 2D algorithm
#include <bits/stdc++.h>
#define mx 1<<31
using namespace std;
int arr[101][101];
int brr[101];
int main()
{
int n;
while(cin >> n)
{
for(int i = 0; i < n; i++)
{
for(int j =0 ; j < n; j++)
{
cin >> arr[i][j];
}
}
int mxx = mx, mxxx;
for(int i = 0; i < n; i++)
{
memset(brr, 0, sizeof brr);
for(int j = i; j < n; j++)
{
int ans = 0 ,a = 0, b = 0, c = 0, d = 0, mxxx = mx;
for(int k = 0; k < n; k++)
{
brr[k] = brr[k] + arr[j][k];
ans += brr[k];
if(ans > mxxx)
{
mxxx= ans;
}
if(ans < 0)
{
ans = 0;
}
}
if(mxxx > mxx)
mxx = mxxx;
}
}
cout<<mxx<< endl;
}
}