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 dp[5005][5005];
int pref[5005][5005];
int main() {
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; ++i) cin >> arr[i];
for (int i = 1; i < n; ++i) {
int cur = 0, prev = 0, v = 0;
int i2 = i;
for (int j = i; j < n; ++j) {
cur += arr[j];
while (i2 && prev < cur - arr[j]) {
--i2;
prev += arr[i2];
}
if (i2 && prev == cur - arr[j]) {
if (prev) ++v;
--i2;
prev += arr[i2];
}
dp[i][j] = pref[i - 1][i2] + v;
if (i != j) dp[i][j] = max(dp[i][j], dp[i][j - 1]);
pref[j][i] = max(pref[j][i], dp[i][j]);
}
}
int ans = 0;
for (int i = 0; i < n; ++i) ans = max(ans, dp[i][n - 1]);
cout << ans << endl;
}
# | 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... |