#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int a[5050], dp[5050][5050];
int main(){
int n;
scanf("%d", &n);
for (int i=1;i<=n;i++) scanf("%d", a+i);
for (int s=2;s<=n;s++){
//printf("ok %d\n", s);
int L[2] = {a[s-1], a[s]};
int pt[2] = {s-2, s+1};
vector<pair<int, int>> P;
while(pt[0] > 0 && pt[1] <= n){
if (pt[0] > 0 && pt[1] <= n && L[0]==L[1]){
P.emplace_back(pt[0], pt[1]);
}
if (L[0] < L[1]) {L[0] += a[pt[0]]; pt[0]--;}
else {L[1] += a[pt[1]]; pt[1]++;}
}
//if (s==9) for (auto &[x, y]:P) printf(" %d %d\n", x, y);
//printf("okok\n");
vector<pair<int, int>> st; ///(count, val)
int cnt = 0, pt2 = 0;
for (int i=s-1;i>0;i--){
if (pt2<(int)P.size() && P[pt2].first==i) {cnt++; pt2++;}
int val = dp[i][s-1] + cnt;
if (!st.empty() && (st.back().first < cnt && st.back().second >= val)) continue;
if (st.empty() || st.back().first < cnt) st.emplace_back(cnt, val);
else if (st.back().second < val) st.back().second = val;
}
//printf("okokok\n");
pt2 = (int)P.size()-1;
for (int j=n;j>=s;j--){
assert(!st.empty());
dp[s][j] = st.back().second;
if (pt2 >= 0 && P[pt2].second==j){
cnt = pt2;
if (st.back().first == cnt+1){
st.back().first--;
st.back().second--;
if (st.size() >= 2){
auto p = *++st.rbegin();
if (p.first==cnt){
p.second = max(p.second, st.back().second);
st.pop_back();
st.pop_back();
st.push_back(p);
}
else if (p.second >= st.back().second){
st.pop_back();
}
}
}
--pt2;
}
}
}
//printf("%d\n", dp[3][8]);
int ans = 0;
for (int i=1;i<=n;i++) ans = max(ans, dp[i][n]);
printf("%d\n", ans);
return 0;
}
Compilation message
cigle.cpp: In function 'int main()':
cigle.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
cigle.cpp:10:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | for (int i=1;i<=n;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
560 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2772 KB |
Output is correct |
2 |
Correct |
3 ms |
2736 KB |
Output is correct |
3 |
Correct |
3 ms |
2712 KB |
Output is correct |
4 |
Correct |
3 ms |
2724 KB |
Output is correct |
5 |
Correct |
2 ms |
2744 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
3 ms |
2772 KB |
Output is correct |
9 |
Correct |
4 ms |
2772 KB |
Output is correct |
10 |
Correct |
3 ms |
2772 KB |
Output is correct |
11 |
Correct |
3 ms |
2740 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
560 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
568 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
3 ms |
2736 KB |
Output is correct |
23 |
Correct |
3 ms |
2712 KB |
Output is correct |
24 |
Correct |
3 ms |
2724 KB |
Output is correct |
25 |
Correct |
2 ms |
2744 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2740 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
4 ms |
2736 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
4 ms |
2792 KB |
Output is correct |
38 |
Correct |
3 ms |
2772 KB |
Output is correct |
39 |
Correct |
3 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
3 ms |
2736 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
2 ms |
2772 KB |
Output is correct |
47 |
Correct |
4 ms |
2772 KB |
Output is correct |
48 |
Correct |
3 ms |
2736 KB |
Output is correct |
49 |
Correct |
3 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
560 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
568 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
3 ms |
2736 KB |
Output is correct |
23 |
Correct |
3 ms |
2712 KB |
Output is correct |
24 |
Correct |
3 ms |
2724 KB |
Output is correct |
25 |
Correct |
2 ms |
2744 KB |
Output is correct |
26 |
Correct |
3 ms |
2772 KB |
Output is correct |
27 |
Correct |
3 ms |
2772 KB |
Output is correct |
28 |
Correct |
3 ms |
2772 KB |
Output is correct |
29 |
Correct |
4 ms |
2772 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2740 KB |
Output is correct |
32 |
Correct |
3 ms |
2772 KB |
Output is correct |
33 |
Correct |
4 ms |
2736 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
3 ms |
2772 KB |
Output is correct |
37 |
Correct |
4 ms |
2792 KB |
Output is correct |
38 |
Correct |
3 ms |
2772 KB |
Output is correct |
39 |
Correct |
3 ms |
2772 KB |
Output is correct |
40 |
Correct |
2 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
3 ms |
2736 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2772 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
2 ms |
2772 KB |
Output is correct |
47 |
Correct |
4 ms |
2772 KB |
Output is correct |
48 |
Correct |
3 ms |
2736 KB |
Output is correct |
49 |
Correct |
3 ms |
2772 KB |
Output is correct |
50 |
Correct |
168 ms |
67408 KB |
Output is correct |
51 |
Correct |
170 ms |
67460 KB |
Output is correct |
52 |
Correct |
170 ms |
67428 KB |
Output is correct |
53 |
Correct |
161 ms |
67520 KB |
Output is correct |
54 |
Correct |
162 ms |
67436 KB |
Output is correct |
55 |
Correct |
166 ms |
67404 KB |
Output is correct |
56 |
Correct |
234 ms |
67480 KB |
Output is correct |
57 |
Correct |
234 ms |
67376 KB |
Output is correct |
58 |
Correct |
219 ms |
67520 KB |
Output is correct |
59 |
Correct |
197 ms |
67468 KB |
Output is correct |
60 |
Correct |
184 ms |
67376 KB |
Output is correct |
61 |
Correct |
178 ms |
67440 KB |
Output is correct |
62 |
Correct |
168 ms |
67384 KB |
Output is correct |
63 |
Correct |
159 ms |
67436 KB |
Output is correct |
64 |
Correct |
166 ms |
67468 KB |
Output is correct |
65 |
Correct |
163 ms |
67416 KB |
Output is correct |
66 |
Correct |
172 ms |
67484 KB |
Output is correct |
67 |
Correct |
165 ms |
67364 KB |
Output is correct |
68 |
Correct |
155 ms |
67400 KB |
Output is correct |
69 |
Correct |
158 ms |
67484 KB |
Output is correct |
70 |
Correct |
176 ms |
67412 KB |
Output is correct |
71 |
Correct |
195 ms |
67424 KB |
Output is correct |
72 |
Correct |
189 ms |
67440 KB |
Output is correct |