Pagini recente » Cod sursa (job #780113) | Cod sursa (job #554537) | Cod sursa (job #2716284) | Cod sursa (job #2503765) | Cod sursa (job #756308)
Cod sursa(job #756308)
#include <cstdio>
#include <algorithm>
#define NMax 35000
#define SqrtT 400
#define LL long long
using namespace std;
int N, NInt;
LL DP[NMax], T, S[NMax], Day[NMax];
void Read ()
{
freopen ("euro.in", "r", stdin);
scanf ("%d %lld", &N, &T);
LL CurrentS=0, X;
for (int i=1; i<N; ++i)
{
scanf ("%lld", &X); CurrentS+=X;
if (CurrentS<0)
{
S[++NInt]=CurrentS; Day[NInt]=i;
CurrentS=0;
}
}
scanf ("%lld", &X);
CurrentS+=X;
S[++NInt]=CurrentS; Day[NInt]=N;
}
void Solve ()
{
for (int i=1; i<=NInt; ++i)
{
DP[i]=DP[i-1]+S[i]*Day[i];
LL CurrentS=S[i];
for (int j=i-1; j>i-SqrtT and j>0; --j)
{
CurrentS+=S[j];
DP[i]=max (DP[i], DP[j-1]+CurrentS*Day[i]);
}
DP[i]-=T;
}
}
void Print ()
{
freopen ("euro.out", "w", stdout);
printf ("%lld\n", DP[NInt]);
}
int main ()
{
Read ();
Solve ();
Print ();
return 0;
}