Cod sursa(job #662429)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 16 ianuarie 2012 18:26:47
Problema Euro Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>
#include <algorithm>

#define NMax 35000
#define SqrtT 400

using namespace std;

int N, NInt;
long long DP[NMax], T, S[NMax], Day[NMax];

void Read ()
{
    freopen ("euro.in", "r", stdin);
    scanf ("%d %lld", &N, &T);
    long long 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];
        long long 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;
}