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;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> d(n);
for (int i = 0; i < n; ++i) {
cin >> d[i];
}
vector dp(n, vector<int>(n + 1)), pref(dp);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
pref[i][j + 1] = max(pref[i][j], dp[i][j]);
}
int bal = 0, mx = 0, cnt = -1;
for (int k = i + 1, j = i; k < n; ++k) {
int add = 0;
if (bal == 0) {
add = 1;
cnt += 1;
}
bal += d[k];
while (j >= 0 && bal - d[j] >= 0) {
mx = max(mx, cnt + dp[i][j] - add * (j == i));
bal -= d[j--];
}
dp[k][i + 1] = mx;
if (j >= 0) {
dp[k][i + 1] = max(dp[k][i + 1], cnt + pref[i][j + 1]);
}
}
}
cout << *max_element(dp[n - 1].begin(), dp[n - 1].end()) << '\n';
return 0;
}
# | 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... |