Cod sursa(job #1267445)

Utilizator teoionescuIonescu Teodor teoionescu Data 19 noiembrie 2014 21:42:42
Problema Euro Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include<fstream>
using namespace std;
ifstream in("euro.in");
ofstream out("euro.out");
const int Nmax = 35001;
const int INF = 0x3f3f3f3f;
long long F[Nmax],D[Nmax];
int N,T,d[Nmax],st=1,dr=1;
int main(){
    in>>N>>T;
    for(int i=1;i<=N;i++) in>>F[i] , F[i]+=F[i-1];
    for(int i=1;i<=N;i++){
        D[i]=-INF;
        while(st<dr && (D[d[st+1]]-D[d[st]]) >= i * (F[d[st+1]]-F[d[st]]) ) st++;
        D[i] = D[ d[st] ] + i*(F[i]-F[ d[st] ]) - T;
        while(st<dr && (D[i]-D[d[dr]]) >= i * (F[i]-F[d[dr]]) ) dr--;
        d[++dr]=i;
    }
    out<<D[N]<<'\n';
    return 0;
}