Cod sursa(job #2622686)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 1 iunie 2020 17:54:29
Problema Lupul Urias si Rau Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <bits/stdc++.h>

using namespace std;

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

constexpr int NMAX = 1e5 + 5;

int N, X, L;

pair <int, int> a[NMAX];

void Read ()
{
    f >> N >> X >> L;

    for (int i = 1; i <= N; ++i)
    {
        f >> a[i].first >> a[i].second;

        if (a[i].first > X) a[i].first = 0;
        else a[i].first = (X - a[i].first) / L + 1;
    }

    sort(a+1, a+N+1);
}

void Solve ()
{
    priority_queue <int> H;

    int val = a[N].first;
    long long sol = 0;

    for (int i = N; i >= 1 && val; --val)
    {
        while (a[i].first == val)
        {
            H.push(a[i].second);

            --i;
        }

        if (!H.empty())
        {
            sol += 1LL * H.top();

            H.pop();
        }
        else val = a[i].first + 1;
    }

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

int main()
{
    Read();

    Solve();

    return 0;
}