Cod sursa(job #843148)

Utilizator stoicatheoFlirk Navok stoicatheo Data 27 decembrie 2012 15:09:10
Problema Euro Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#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;
}