Cod sursa(job #1337041)

Utilizator margikiMargeloiu Andrei margiki Data 8 februarie 2015 15:40:29
Problema Branza Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
# include <fstream>
# include <deque>
# include <algorithm>
# define NR 100005
using namespace std;
ifstream f("branza.in");
ofstream g("branza.out");
deque <int> d;
int i,j,n,m,S,T;
int c[NR], p[NR];
long long cost;
int main ()
{
    f>>n>>S>>T;

    for (i=1; i<=n; ++i)
    {
        f>>c[i]>>p[i];//costul si productia
        while (! d.empty() && c[i]*p[i]<=c[d.back()]*p[i]+S*p[i]*(i-d.back()))
             d.pop_back();
        d.push_back(i);
        if (d.front()==i-T) d.pop_front();

        cost+=(p[i]*c[d.front()]+S*p[i]*(i-d.front()));
    }
    g<<cost<<"\n";
    return 0;
}