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;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e5+5;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define fr first
#define sc second
int n, v[5005], dp[5005][5005], max_dp[5005][5005], pref[5005]; // max_dp[i][j] = max{dp[1][j], dp[2][j], ..., dp[i][j]}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> v[i], pref[i] = pref[i-1] + v[i];
// l ... r
// l-1 ... k
for(int l = 1; l <= n; l++) {
dp[l][l] = max_dp[l-1][l-1];
int cnt = 0;
for(int r = l, k = l-1; r <= n; r++) {
if(r > l) dp[l][r] = dp[l][r-1];
if(k >= 1) dp[l][r] = max(dp[l][r], max_dp[k][l-1] + cnt);
while(k >= 1 and pref[l-1] - pref[k-1] < pref[r] - pref[l-1]) k--;
if(k >= 1 and pref[l-1] - pref[k-1] == pref[r] - pref[l-1]) cnt++, k--;
}
for(int k = 1; k <= l; k++)
max_dp[k][l] = max(dp[k][l], max_dp[k-1][l]);
}
cout << max_dp[n][n] << "\n";
}
# | 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... |