Cod sursa(job #2731809)

Utilizator vlad_dimaVlad Dima vlad_dima Data 28 martie 2021 12:50:47
Problema Branza Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ifstream fin("branza.in");
    ofstream fout("branza.out");
    int n, s, t, t2 = 0, kg_depoz = 0;
    long long sum = 0;
    fin>>n>>s>>t;
    stack <int> cost;
    queue <int> cant;
    while (n)
    {
        int c, p;
        fin>>c>>p;
//        cout<<c<<' '<<p<<endl;
        n--;
        if (cost.empty())
        {
            sum += c * p;
            cost.push(c);
        }
        else if (t2 <= t && cost.top() * p + s * p < c * p)
        {
            kg_depoz += p;
            cant.push(p);
            t2++;
        }
        else if (t2 > 0)
        {
            sum += cost.top() * kg_depoz;
            while (!cant.empty())
                kg_depoz -= cant.front(), sum += kg_depoz * s, cant.pop();
            cost.pop();
            kg_depoz = 0;
        }
        else
            {
                cost.pop();
                sum += c * p;
                cost.push(c);
            }
    }
    if (t2 > 0)
        {
            sum += cost.top() * kg_depoz;
            sum += s * kg_depoz;
            while (!cant.empty())
                kg_depoz -= cant.front(), sum += kg_depoz * s, cant.pop();
        }
    fout<<sum;
}