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 rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define pii pair<int,int>
#define vi vector<int>
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define ll long long
using namespace std;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int N=5e3+5;
int a[N],dp[N][N],g[N][N],pos[N][N];
void Max(int &a,const int b){if(a<b)a=b;}
int main(){
int n=read();
rep(i,1,n)a[i]=read();
rep(i,1,n){
int sum=0,pre=0,p=i-1;
rep(j,i,n){
dp[i][j]=dp[i-1][i-1];
g[i][j]=g[i][j-1];
sum+=a[j];
while(p&&pre+a[p]<=sum)pre+=a[p--];
if(sum==pre)g[i][j]++;
pos[i][j]=p;
if(pos[i][j-1])Max(dp[i][j-1],dp[pos[i][j-1]][i-1]+g[i][j-1]);
Max(dp[i][j],dp[i][j-1]);
Max(dp[i][j],dp[i-1][j]);
}
}
printf("%d\n",dp[n][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... |