#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
const int inf = 1e16;
const int N = 5e3 + 5;
vector<int> a(N);
vector<pair<int, int> > prec[N];
int add(int l, int r, int x){
if(!prec[r].size() || (prec[r][0].first <= l || prec[r][0].second >= x)) return 0;
int lb = 0, rb = prec[r].size()-1;
while(lb < rb){
int mid = (lb + rb + 1)/2;
if(prec[r][mid].first > l && prec[r][mid].second < x) lb = mid;
else rb = mid - 1;
}
return lb + 1;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// dp[son kat l][son kat r] -> dp[r + 1][x] + match({l, r} , {r+1, x});
int n;
cin>>n;
for(int i = 0; i < n; i++){
cin>>a[i];
}
for(int i = 1; i < n-1; i++){
vector<pair<int, int> > v1, v2;
int val = 0;
for(int j = i; j > 0; j--){
val += a[j];
v1.pb({val, j});
}
val = 0;
for(int j = i+1; j < n-1; j++){
val += a[j];
v2.pb({val, j});
}
int ind2 = 0;
for(int j = 0; j < v1.size(); j++){
while(ind2 < v2.size() && v2[ind2].first < v1[j].first) ind2++;
if(ind2 < v2.size() && v2[ind2].first == v1[j].first){
prec[i].pb({v1[j].second, v2[ind2].second});
}
}
}
vector<vector<int> > dp(n, vector<int>(n, -inf));
for(int i = 0; i < n; i++){
dp[0][i] = 0;
}
int ans = 0;
for(int l = 0; l < n; l++){
for(int r = l; r < n; r++){
for(int x = r+1; x < n; x++){
dp[r+1][x] = max(dp[r+1][x], dp[l][r] + add(l, r, x));
}
}
ans = max(ans, dp[l][n-1]);
}
cout<<ans<<endl;
return 0;
}
Compilation message
cigle.cpp: In function 'int32_t main()':
cigle.cpp:53:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int j = 0; j < v1.size(); j++){
| ~~^~~~~~~~~~~
cigle.cpp:54:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | while(ind2 < v2.size() && v2[ind2].first < v1[j].first) ind2++;
| ~~~~~^~~~~~~~~~~
cigle.cpp:55:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | if(ind2 < v2.size() && v2[ind2].first == v1[j].first){
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
764 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
3152 KB |
Output is correct |
2 |
Correct |
264 ms |
3708 KB |
Output is correct |
3 |
Correct |
223 ms |
3424 KB |
Output is correct |
4 |
Correct |
260 ms |
3524 KB |
Output is correct |
5 |
Correct |
254 ms |
3580 KB |
Output is correct |
6 |
Correct |
264 ms |
3408 KB |
Output is correct |
7 |
Correct |
261 ms |
3548 KB |
Output is correct |
8 |
Correct |
268 ms |
3408 KB |
Output is correct |
9 |
Correct |
262 ms |
3408 KB |
Output is correct |
10 |
Correct |
232 ms |
3408 KB |
Output is correct |
11 |
Correct |
243 ms |
3152 KB |
Output is correct |
12 |
Correct |
225 ms |
3152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
764 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
204 ms |
3152 KB |
Output is correct |
22 |
Correct |
264 ms |
3708 KB |
Output is correct |
23 |
Correct |
223 ms |
3424 KB |
Output is correct |
24 |
Correct |
260 ms |
3524 KB |
Output is correct |
25 |
Correct |
254 ms |
3580 KB |
Output is correct |
26 |
Correct |
264 ms |
3408 KB |
Output is correct |
27 |
Correct |
261 ms |
3548 KB |
Output is correct |
28 |
Correct |
268 ms |
3408 KB |
Output is correct |
29 |
Correct |
262 ms |
3408 KB |
Output is correct |
30 |
Correct |
232 ms |
3408 KB |
Output is correct |
31 |
Correct |
243 ms |
3152 KB |
Output is correct |
32 |
Correct |
225 ms |
3152 KB |
Output is correct |
33 |
Correct |
203 ms |
3168 KB |
Output is correct |
34 |
Correct |
233 ms |
3420 KB |
Output is correct |
35 |
Correct |
226 ms |
3408 KB |
Output is correct |
36 |
Correct |
260 ms |
3408 KB |
Output is correct |
37 |
Correct |
273 ms |
3408 KB |
Output is correct |
38 |
Correct |
269 ms |
3408 KB |
Output is correct |
39 |
Correct |
182 ms |
2640 KB |
Output is correct |
40 |
Correct |
56 ms |
2552 KB |
Output is correct |
41 |
Correct |
70 ms |
2384 KB |
Output is correct |
42 |
Correct |
89 ms |
2384 KB |
Output is correct |
43 |
Correct |
80 ms |
2384 KB |
Output is correct |
44 |
Correct |
62 ms |
2384 KB |
Output is correct |
45 |
Correct |
77 ms |
2600 KB |
Output is correct |
46 |
Correct |
72 ms |
2384 KB |
Output is correct |
47 |
Correct |
62 ms |
2592 KB |
Output is correct |
48 |
Correct |
75 ms |
2384 KB |
Output is correct |
49 |
Correct |
77 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
764 KB |
Output is correct |
4 |
Correct |
1 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
1 ms |
592 KB |
Output is correct |
8 |
Correct |
1 ms |
764 KB |
Output is correct |
9 |
Correct |
1 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
13 |
Correct |
2 ms |
592 KB |
Output is correct |
14 |
Correct |
2 ms |
592 KB |
Output is correct |
15 |
Correct |
2 ms |
592 KB |
Output is correct |
16 |
Correct |
1 ms |
592 KB |
Output is correct |
17 |
Correct |
1 ms |
592 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
764 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
204 ms |
3152 KB |
Output is correct |
22 |
Correct |
264 ms |
3708 KB |
Output is correct |
23 |
Correct |
223 ms |
3424 KB |
Output is correct |
24 |
Correct |
260 ms |
3524 KB |
Output is correct |
25 |
Correct |
254 ms |
3580 KB |
Output is correct |
26 |
Correct |
264 ms |
3408 KB |
Output is correct |
27 |
Correct |
261 ms |
3548 KB |
Output is correct |
28 |
Correct |
268 ms |
3408 KB |
Output is correct |
29 |
Correct |
262 ms |
3408 KB |
Output is correct |
30 |
Correct |
232 ms |
3408 KB |
Output is correct |
31 |
Correct |
243 ms |
3152 KB |
Output is correct |
32 |
Correct |
225 ms |
3152 KB |
Output is correct |
33 |
Correct |
203 ms |
3168 KB |
Output is correct |
34 |
Correct |
233 ms |
3420 KB |
Output is correct |
35 |
Correct |
226 ms |
3408 KB |
Output is correct |
36 |
Correct |
260 ms |
3408 KB |
Output is correct |
37 |
Correct |
273 ms |
3408 KB |
Output is correct |
38 |
Correct |
269 ms |
3408 KB |
Output is correct |
39 |
Correct |
182 ms |
2640 KB |
Output is correct |
40 |
Correct |
56 ms |
2552 KB |
Output is correct |
41 |
Correct |
70 ms |
2384 KB |
Output is correct |
42 |
Correct |
89 ms |
2384 KB |
Output is correct |
43 |
Correct |
80 ms |
2384 KB |
Output is correct |
44 |
Correct |
62 ms |
2384 KB |
Output is correct |
45 |
Correct |
77 ms |
2600 KB |
Output is correct |
46 |
Correct |
72 ms |
2384 KB |
Output is correct |
47 |
Correct |
62 ms |
2592 KB |
Output is correct |
48 |
Correct |
75 ms |
2384 KB |
Output is correct |
49 |
Correct |
77 ms |
2384 KB |
Output is correct |
50 |
Execution timed out |
1059 ms |
279388 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |