Cod sursa(job #2487489)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 4 noiembrie 2019 20:47:56
Problema Lupul Urias si Rau Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("lupu.in");
ofstream g ("lupu.out");

struct sheep
{
    int d, w;
};

sheep a[100005];

int n, x, l;
long long sol;

bool cmp (sheep a, sheep b)
{
    return a.d < b.d;
}

priority_queue <int> q;

int main()
{
    f >> n >> x >> l;

    for (int i=1; i<=n; ++i)
    {
        f >> a[i].d >> a[i].w;

        if (a[i].d > x) a[i].d = 0;
        else a[i].d = ((x - a[i].d) / l + 1);
    }

    sort(a+1, a+n+1, cmp);

    int val = a[n].d;

    for (int ind = n; ind >= 1 && val; --val)
    {
        while (val == a[ind].d && ind > 0)
        {
            q.push(a[ind].w);
            ind--;
        }

        if (!q.empty())
        {
            sol = sol + 1LL * q.top();
            q.pop();
        }
        else val = a[ind].d + 1;
    }

    g << sol << '\n';
    return 0;
}