-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13398.cpp
41 lines (41 loc) · 980 Bytes
/
13398.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
#include <bits/stdc++.h>
using namespace std;
int n;
int MIN = -1000000000;
int arr[100000];
int memo[100000][2], ans = MIN;
int solve(int start, int removed) {
if(removed == 2) return MIN;
if(removed == 3) return 0;
if(start == n) return 0;
int &ret = memo[start][removed];
if(ret != MIN) return ret;
ret = max(arr[start] + solve(start + 1, removed), solve(start + 1, removed + 1));
ret = max(0, ret);
return ret;
}
int main() {
cin >> n;
bool flag = 1;
int maxnum = -10000000;
for(int i = 0; i < n; i++) {
cin >> arr[i];
if(arr[i] > 0) flag = 0;
maxnum = max(maxnum, arr[i]);
}
if(flag) {
cout << maxnum;
return 0;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2; j++) {
memo[i][j] = MIN;
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < 2; j++) {
ans = max(ans, solve(i, j));
}
}
cout << ans;
}