#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 5e3 + 5;
vector<int> a(N);
struct SEGT{
vector<int> tree, lazy;
void init(int n){
tree.assign(4*n, 0);
lazy.assign(4*n, 0);
}
void push(int ind){
if(lazy[ind] == 0) return;
lazy[ind*2] += lazy[ind];
tree[ind*2] += lazy[ind];
lazy[ind*2+1] += lazy[ind];
tree[ind*2+1] += lazy[ind];
lazy[ind] = 0;
}
void build(int ind, int l, int r, vector<int> &a){
if(l == r){
tree[ind] = a[l];
}
else{
int m = (l + r)/2;
build(ind*2, l, m, a);
build(ind*2+1, m+1, r, a);
tree[ind] = max(tree[ind*2], tree[ind*2+1]);
}
}
void update(int ind, int l, int r, int ql, int qr, int val){
if(l > r || r < ql || l > qr || ql > qr) return;
if(l >= ql && r <= qr){
tree[ind] += val;
lazy[ind] += val;
}
else{
push(ind);
int m = (l + r)/2;
update(ind*2, l, m, ql, qr, val);
update(ind*2+1, m+1, r, ql, qr, val);
tree[ind] = max(tree[ind*2], tree[ind*2+1]);
}
}
int query(int ind, int l, int r, int ql, int qr){
if(l > r || r < ql || l > qr || ql > qr) return 0;
if(l >= ql && r <= qr){
return tree[ind];
}
else{
push(ind);
int m = (l + r)/2;
return max(query(ind*2, l, m, ql, qr), query(ind*2+1, m+1, r, ql, qr));
}
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
cin>>a[i];
}
vector<pair<int, int> > upd[n+5];
for(int i = 2; i <= n-1; i++){
vector<int> v1, v2;
int val = 0;
for(int j = i; j > 1; j--){
val += a[j];
v1.pb(val); // i, i-1, i-2, ...
}
val = 0;
for(int j = i+1; j <= n-1; j++){
val += a[j];
v2.pb(val); // i+1, i+2, ...
}
int ind2 = 0;
for(int j = 0; j < v1.size(); j++){
while(ind2 < v2.size() && v2[ind2] < v1[j]) ind2++;
if(ind2 < v2.size() && v2[ind2] == v1[j]){
upd[i].pb({ind2 + i + 1, i - j});
}
}
sort(upd[i].begin(), upd[i].end());
}
vector<vector<int> > dp(n+5, vector<int>(n+5));
vector<int> arr(n+1);
for(int r = 1; r <= n-1; r++){
sort(upd[r].begin(), upd[r].end());
int updind = 0;
SEGT seg;
seg.init(r);
for(int i = 1; i <= r; i++) arr[i] = dp[i][r];
seg.build(1, 1, r, arr);
for(int x = r+1; x <= n; x++){
while(updind < upd[r].size() && upd[r][updind].first < x){
seg.update(1, 1, r, 1, upd[r][updind].second-1, 1);
updind++;
}
dp[r+1][x] = seg.tree[1];
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
ans = max(ans, dp[i][n]);
}
cout<<ans<<endl;
return 0;
}
Compilation message
cigle.cpp: In function 'int main()':
cigle.cpp:98:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | for(int j = 0; j < v1.size(); j++){
| ~~^~~~~~~~~~~
cigle.cpp:99:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | while(ind2 < v2.size() && v2[ind2] < v1[j]) ind2++;
| ~~~~~^~~~~~~~~~~
cigle.cpp:100:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | if(ind2 < v2.size() && v2[ind2] == v1[j]){
| ~~~~~^~~~~~~~~~~
cigle.cpp:121:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
121 | while(updind < upd[r].size() && upd[r][updind].first < x){
| ~~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1872 KB |
Output is correct |
2 |
Correct |
7 ms |
2128 KB |
Output is correct |
3 |
Correct |
7 ms |
1872 KB |
Output is correct |
4 |
Correct |
9 ms |
2028 KB |
Output is correct |
5 |
Correct |
7 ms |
1872 KB |
Output is correct |
6 |
Correct |
8 ms |
1872 KB |
Output is correct |
7 |
Correct |
8 ms |
1872 KB |
Output is correct |
8 |
Correct |
8 ms |
2052 KB |
Output is correct |
9 |
Correct |
7 ms |
1872 KB |
Output is correct |
10 |
Correct |
7 ms |
1872 KB |
Output is correct |
11 |
Correct |
8 ms |
1872 KB |
Output is correct |
12 |
Correct |
7 ms |
1872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
6 ms |
1872 KB |
Output is correct |
22 |
Correct |
7 ms |
2128 KB |
Output is correct |
23 |
Correct |
7 ms |
1872 KB |
Output is correct |
24 |
Correct |
9 ms |
2028 KB |
Output is correct |
25 |
Correct |
7 ms |
1872 KB |
Output is correct |
26 |
Correct |
8 ms |
1872 KB |
Output is correct |
27 |
Correct |
8 ms |
1872 KB |
Output is correct |
28 |
Correct |
8 ms |
2052 KB |
Output is correct |
29 |
Correct |
7 ms |
1872 KB |
Output is correct |
30 |
Correct |
7 ms |
1872 KB |
Output is correct |
31 |
Correct |
8 ms |
1872 KB |
Output is correct |
32 |
Correct |
7 ms |
1872 KB |
Output is correct |
33 |
Correct |
6 ms |
1872 KB |
Output is correct |
34 |
Correct |
11 ms |
1780 KB |
Output is correct |
35 |
Correct |
8 ms |
1872 KB |
Output is correct |
36 |
Correct |
8 ms |
1872 KB |
Output is correct |
37 |
Correct |
8 ms |
1872 KB |
Output is correct |
38 |
Correct |
11 ms |
1872 KB |
Output is correct |
39 |
Correct |
6 ms |
1616 KB |
Output is correct |
40 |
Correct |
4 ms |
1360 KB |
Output is correct |
41 |
Correct |
4 ms |
1360 KB |
Output is correct |
42 |
Correct |
4 ms |
1360 KB |
Output is correct |
43 |
Correct |
4 ms |
1372 KB |
Output is correct |
44 |
Correct |
4 ms |
1360 KB |
Output is correct |
45 |
Correct |
4 ms |
1360 KB |
Output is correct |
46 |
Correct |
4 ms |
1360 KB |
Output is correct |
47 |
Correct |
4 ms |
1360 KB |
Output is correct |
48 |
Correct |
4 ms |
1360 KB |
Output is correct |
49 |
Correct |
4 ms |
1360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
592 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
6 ms |
1872 KB |
Output is correct |
22 |
Correct |
7 ms |
2128 KB |
Output is correct |
23 |
Correct |
7 ms |
1872 KB |
Output is correct |
24 |
Correct |
9 ms |
2028 KB |
Output is correct |
25 |
Correct |
7 ms |
1872 KB |
Output is correct |
26 |
Correct |
8 ms |
1872 KB |
Output is correct |
27 |
Correct |
8 ms |
1872 KB |
Output is correct |
28 |
Correct |
8 ms |
2052 KB |
Output is correct |
29 |
Correct |
7 ms |
1872 KB |
Output is correct |
30 |
Correct |
7 ms |
1872 KB |
Output is correct |
31 |
Correct |
8 ms |
1872 KB |
Output is correct |
32 |
Correct |
7 ms |
1872 KB |
Output is correct |
33 |
Correct |
6 ms |
1872 KB |
Output is correct |
34 |
Correct |
11 ms |
1780 KB |
Output is correct |
35 |
Correct |
8 ms |
1872 KB |
Output is correct |
36 |
Correct |
8 ms |
1872 KB |
Output is correct |
37 |
Correct |
8 ms |
1872 KB |
Output is correct |
38 |
Correct |
11 ms |
1872 KB |
Output is correct |
39 |
Correct |
6 ms |
1616 KB |
Output is correct |
40 |
Correct |
4 ms |
1360 KB |
Output is correct |
41 |
Correct |
4 ms |
1360 KB |
Output is correct |
42 |
Correct |
4 ms |
1360 KB |
Output is correct |
43 |
Correct |
4 ms |
1372 KB |
Output is correct |
44 |
Correct |
4 ms |
1360 KB |
Output is correct |
45 |
Correct |
4 ms |
1360 KB |
Output is correct |
46 |
Correct |
4 ms |
1360 KB |
Output is correct |
47 |
Correct |
4 ms |
1360 KB |
Output is correct |
48 |
Correct |
4 ms |
1360 KB |
Output is correct |
49 |
Correct |
4 ms |
1360 KB |
Output is correct |
50 |
Correct |
847 ms |
140548 KB |
Output is correct |
51 |
Correct |
833 ms |
140696 KB |
Output is correct |
52 |
Correct |
828 ms |
141080 KB |
Output is correct |
53 |
Correct |
830 ms |
141012 KB |
Output is correct |
54 |
Correct |
814 ms |
141220 KB |
Output is correct |
55 |
Correct |
861 ms |
141060 KB |
Output is correct |
56 |
Correct |
874 ms |
143752 KB |
Output is correct |
57 |
Correct |
918 ms |
143676 KB |
Output is correct |
58 |
Correct |
530 ms |
111888 KB |
Output is correct |
59 |
Correct |
387 ms |
102236 KB |
Output is correct |
60 |
Correct |
357 ms |
99472 KB |
Output is correct |
61 |
Correct |
345 ms |
99456 KB |
Output is correct |
62 |
Correct |
347 ms |
99600 KB |
Output is correct |
63 |
Correct |
331 ms |
100008 KB |
Output is correct |
64 |
Correct |
326 ms |
99808 KB |
Output is correct |
65 |
Correct |
316 ms |
99788 KB |
Output is correct |
66 |
Correct |
316 ms |
99724 KB |
Output is correct |
67 |
Correct |
330 ms |
99768 KB |
Output is correct |
68 |
Correct |
305 ms |
99880 KB |
Output is correct |
69 |
Correct |
319 ms |
100016 KB |
Output is correct |
70 |
Correct |
327 ms |
99768 KB |
Output is correct |
71 |
Correct |
335 ms |
99512 KB |
Output is correct |
72 |
Correct |
368 ms |
99800 KB |
Output is correct |