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;
#define int long long
#define fi first
#define se second
#define pb push_back
#define debug(x) cout<<#x<<": "<<x<<"\n"
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// freopen("input.000","r",stdin);
// freopen("output.000","w",stdout);
// srand((unsigned)time(NULL));
// rand()
int n,i,a[5005],dp[5005][5005],j,cnt,l,diff,optl,ans,dpmax[5005][5005];
cin>>n;
for (i=1;i<n+1;i++) cin>>a[i];
for (j=1;j<n+1;j++) dp[0][j]=dp[1][j]=0;
for (j=1;j<n+1;j++) dpmax[0][j]=dpmax[1][j]=0;
ans=0;
for (i=2;i<n;i++)
{
cnt=0;l=i-1;diff=0;optl=i-1;
dp[i][i+1]=dpmax[i-1][i];
dpmax[i][i+1]=max(dpmax[i-1][i+1],dp[i][i+1]);
for (j=i+2;j<n+1;j++)
{
diff -= a[j-1];
while (diff<0 and l>0)
{
diff += a[l+1];
l--;
}
if (diff==0)
{
cnt++;
optl=l;
}
dp[i][j]=max(dp[i][j-1],dpmax[optl][i]+cnt);
dpmax[i][j]=max(dpmax[i-1][j],dp[i][j]);
ans=max(ans,dp[i][j]);
// cout<<i<<" "<<j<<" "<<dp[i][j]<<" "<<optl<<" "<<cnt<<"\n";
}
}
cout<<ans;
}
# | 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... |