#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 5e3 + 7;
constexpr int oo = 1e9;
int dp[MAXN][MAXN][2];
int d[MAXN];
bool sum[MAXN*MAXN];
int res = 0;
int n;
int compute(int j, int k, int i, bool right){
vector<int> tmp[2];
for(int z = j; z <= i; z++)
tmp[(z > k)].push_back(d[z]);
reverse(tmp[0].begin(), tmp[0].end());
int actsum = 0, cnt = 0;
for(int z = 0; z + 1 < tmp[0].size(); z++){
actsum += tmp[0][z];
sum[actsum] = 1;
}
actsum = 0;
for(int z = 0; z + 1 < tmp[1].size(); z++){
actsum += tmp[1][z];
if(sum[actsum]) cnt++;
}
actsum = 0;
for(int z = 0; z + 1 < tmp[0].size(); z++){
actsum += tmp[0][z];
sum[actsum] = 0;
}
if(cnt == 0) return -oo;
//if(cnt > 1) cout << j << " " << k << " " << i << " " << right << " " << cnt << "\n";
return cnt;
}
void solve(){
for(int i = 0; i <= n; i++)
for(int j = 0; j <= n; j++)
dp[i][j][0] = dp[i][j][1] = -oo;
for(int i = 1; i <= n; i++)
dp[1][i][0] = 0;
for(int i = 2; i <= n; i++){
for(int k = i-1; k >= 1; k--){
for(int j = k; j >= 1; j--){
dp[k+1][i][0] = max(dp[k+1][i][0], dp[j][k][1] + compute(j, k, i, 0));
dp[k+1][i][1] = max(dp[k+1][i][1], dp[j][k][0] + compute(j, k, i, 1));
res = max({res, dp[k+1][i][0], dp[k+1][i][1]});
}
}
}
/*
for(int i = 1; i <= n; i++){
for(int k = 1; k <= n; k++){
for(int j = k-1; j <= n; j++){
for(int z = j; z < i; z++){
dp[z+1][i][k] = max(dp[z+1][i][k], dp[j][z][k-1] + compute(j, z, i, (k%2)));
res = max(res, dp[z+1][i][k]);
}
}
}
}
*/
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> d[i];
solve();
cout << res << "\n";
}
Compilation message
cigle.cpp: In function 'int compute(int, int, int, bool)':
cigle.cpp:25:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int z = 0; z + 1 < tmp[0].size(); z++){
| ~~~~~~^~~~~~~~~~~~~~~
cigle.cpp:32:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int z = 0; z + 1 < tmp[1].size(); z++){
| ~~~~~~^~~~~~~~~~~~~~~
cigle.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int z = 0; z + 1 < tmp[0].size(); z++){
| ~~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4600 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4600 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
61 ms |
6492 KB |
Output is correct |
11 |
Correct |
64 ms |
6600 KB |
Output is correct |
12 |
Correct |
70 ms |
6600 KB |
Output is correct |
13 |
Correct |
61 ms |
6492 KB |
Output is correct |
14 |
Correct |
63 ms |
6488 KB |
Output is correct |
15 |
Correct |
62 ms |
6596 KB |
Output is correct |
16 |
Correct |
62 ms |
8788 KB |
Output is correct |
17 |
Correct |
62 ms |
6492 KB |
Output is correct |
18 |
Correct |
62 ms |
6488 KB |
Output is correct |
19 |
Correct |
61 ms |
6488 KB |
Output is correct |
20 |
Correct |
62 ms |
6644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1059 ms |
23016 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4600 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
61 ms |
6492 KB |
Output is correct |
11 |
Correct |
64 ms |
6600 KB |
Output is correct |
12 |
Correct |
70 ms |
6600 KB |
Output is correct |
13 |
Correct |
61 ms |
6492 KB |
Output is correct |
14 |
Correct |
63 ms |
6488 KB |
Output is correct |
15 |
Correct |
62 ms |
6596 KB |
Output is correct |
16 |
Correct |
62 ms |
8788 KB |
Output is correct |
17 |
Correct |
62 ms |
6492 KB |
Output is correct |
18 |
Correct |
62 ms |
6488 KB |
Output is correct |
19 |
Correct |
61 ms |
6488 KB |
Output is correct |
20 |
Correct |
62 ms |
6644 KB |
Output is correct |
21 |
Execution timed out |
1059 ms |
23016 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4600 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
61 ms |
6492 KB |
Output is correct |
11 |
Correct |
64 ms |
6600 KB |
Output is correct |
12 |
Correct |
70 ms |
6600 KB |
Output is correct |
13 |
Correct |
61 ms |
6492 KB |
Output is correct |
14 |
Correct |
63 ms |
6488 KB |
Output is correct |
15 |
Correct |
62 ms |
6596 KB |
Output is correct |
16 |
Correct |
62 ms |
8788 KB |
Output is correct |
17 |
Correct |
62 ms |
6492 KB |
Output is correct |
18 |
Correct |
62 ms |
6488 KB |
Output is correct |
19 |
Correct |
61 ms |
6488 KB |
Output is correct |
20 |
Correct |
62 ms |
6644 KB |
Output is correct |
21 |
Execution timed out |
1059 ms |
23016 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |