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>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int dp[5002][5002], sum[5002], n, pref[5002];
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
sum[i] = sum[i - 1] + a;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++)
pref[j] = max(pref[j - 1], dp[j][i - 1]);
int matches = 0;
int ind = i - 1, best = pref[i - 1];
for (int j = i; j <= n; j++) {
dp[i][j] = best;
while (ind > 0 && sum[j] - sum[i - 1] > sum[i - 1] - sum[ind])
ind--;
if (ind && sum[j] - sum[i - 1] == sum[i - 1] - sum[ind]) {
matches++;
best = max(best, matches + pref[ind - 1]);
}
}
}
int mx = 0;
for (int i = 1; i <= n; i++)
mx = max(mx, dp[i][n]);
cout << mx+1 << "\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... |