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 pb push_back
#define ll long long
using namespace std;
ifstream f("teroristi2.in");
ofstream g("teroristi2.out");
int N;
int h[5005];
int dp[5005][5005];
int sp[5005];
int bruteforce()
{
for(int fin=1; fin<=N; fin++)
{
for(int start=fin; start>=1; start--)
{
vector<int> sp(N+5,0);
for(int j=fin; j>=start; j--)
{
sp[j]=sp[j+1]+h[j];
}
int sum=0,layers=0,cnt=fin;
for(int fin2=fin+1; fin2<=N; fin2++)
{
///dp[fin+1][fin2]=max(dp[fin+1][fin2],dp[start][fin]+layers)
sum+=h[fin2];
dp[fin+1][fin2]=max(dp[fin+1][fin2],dp[start][fin]+layers);
while(sum>sp[cnt]&&cnt>start+1)
{
cnt--;
}
if(sum==sp[cnt]&&cnt!=start)
{
layers++;
}
}
}
}
int rez=0;
for(int i=1; i<=N; i++)
{
rez=max(rez,dp[i][N]);
// cout<<i<<" "<<dp[i][N]<<'\n';
}
return rez;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>N;
for(int i=1; i<=N; i++)
{
cin>>h[i];
sp[i]=sp[i-1]+h[i];
}
for(int fin=1; fin<=N; fin++)
{
vector<int> aux(N+5,0);
for(int start=1;start<=fin;start++)
{
aux[start]=max(aux[start-1],dp[start][fin]);
}
int start=fin-1;
int mxdp=0,layers=0;
for(int fin2=fin+1;fin2<=N;fin2++)
{
dp[fin][fin2]=mxdp;
while(start&&sp[fin2]-sp[fin]>sp[fin]-sp[start])
{
start--;
}
if(start&&sp[fin2]-sp[fin]==sp[fin]-sp[start])
{
layers++;
mxdp=max(mxdp,aux[start-1]+layers);
}
}
}
int rez=0;
for(int i=0; i<N; i++)
{
rez=max(rez,dp[i][N]);
}
cout<<rez;
}
# | 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... |