Cod sursa(job #1384037)

Utilizator thehuntestshadowDragomir Alexandru thehuntestshadow Data 10 martie 2015 20:17:24
Problema Euro Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include<cmath>
#include<fstream>
using namespace std;
#define NMAX 34578
int M;const int L = (1LL << 31) - 1;
long long int DP[NMAX];
int main()
{
    ifstream fin ("euro.in");
    int n,t;
    fin  >> n >> t;
    int arr[n+3],S[NMAX];S[0]=0;
    int V[NMAX];
    for(int i=1;i<=n;++i)
    {
        fin >>arr[i];
        S[i]=S[i-1]+arr[i];
    }int i,j;
for(i = j = 1; i <= n; i++)
        if(S[i] - S[j - 1] < 0) {
            V[++M] = i;
            j = i + 1;
        }

    if(j<=M)
        V[++M]=n;

    for(i=1;i<=M;++i)
    {
        DP[i]=-L;
        for(j=max((int)(i-sqrt(t)-2),1);j<=i;j++)
        {
            DP[i]=max(DP[i],DP[j-1]+(S[V[i]]-S[V[j-1]])*1LL*V[i]-t);
        }
    }
    ofstream fout ("euro.out");
    fout <<-DP[M]<<"\n";
    return 0;
}