This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
vector<int> d(n);
for (int &x : d) {
cin >> x;
}
vector<vector<int>> f(n, vector<int>(n));
for (int i = 0; i < n - 1; i++) {
vector<int> num(n);
int l = i, r = i + 1, s = 0, cnt = 0;
while (~l || r < n) {
if (r == n || s <= 0 && l >= 0) s += d[l--];
else s -= d[r++];
if (!s) cnt++;
if (~l) num[l] = cnt;
if (r < n) num[r] = cnt;
}
vector<int> pre(i + 1);
for (int j = 0; j <= i; j++) {
pre[j] = max(j ? pre[j - 1] : 0, f[j][i]);
}
for (int k = i + 1, j = i, t = 0; k < n; k++) {
for (; ~j && num[j] <= num[k]; j--) {
t = max(t, f[j][i] + num[j]);
}
f[i + 1][k] = max(t, ~j ? pre[j] + num[k] : 0);
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, f[i][n - 1]);
}
cout << ans << "\n";
return 0;
}
Compilation message (stderr)
cigle.cpp: In function 'int main()':
cigle.cpp:17:34: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
17 | if (r == n || s <= 0 && l >= 0) s += d[l--];
| ~~~~~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |