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>
#include <random>
#define ll long long
#define int long long
#define fi first
#define se second
#define pb push_back
#define md ((l+r)>>1)
#define lf (id<<1)
#define rg ((id<<1)|1)
#define ld long double
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,pii> ipii;
const int MAXN = 5e3+10;
const int MAXA = 1e6+10;
const int INF = 2e9+10;
const int LOG = 30;
const int MOD = 1e9+7;
void chmx(int &a, int b){
a = max(a, b);
}
int n;
int a[MAXN], pr[MAXN], dp[MAXN][MAXN];
signed main() {
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i];
}
for(int p=1; p<=n-1; p++){
pr[1] = dp[1][p];
for(int i=2; i<=p; i++){
pr[i] = max(pr[i-1], dp[i][p]);
}
int l = p, r = p+1, top = 0, bot = 0, cnt = -1;
while(l>=1 && r<=n){
if(top==bot){
cnt++;
chmx(dp[p+1][r], pr[l]+cnt); // max dp[...][l-1]
top += a[l--]; bot += a[r++];
} else if(top < bot){
top += a[l--];
} else if(top > bot){
bot += a[r++];
}
}
for(int i=p+1; i<=n; i++) chmx(dp[p][i], dp[p][i-1]);
}
int ANS = 0;
for(int i=1; i<=n; i++) ANS = max(ANS, dp[i][n]);
cout << ANS << '\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... |