Cod sursa(job #3267568)

Utilizator Andercau_VasileAndercau Vasile Andercau_Vasile Data 11 ianuarie 2025 14:02:56
Problema Lupul Urias si Rau Scor 16
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <fstream>
#include <queue>
using namespace std;

ifstream fin("lupu.in");
ofstream fout("lupu.out");

#define NMAX 100005

priority_queue<int> PQ[NMAX];

int main() {
    int n, x, l;
    fin >> n >> x >> l;
    int maxim = 0;
    for (int i = 1; i <= n; ++i) {
        int d, a;
        fin >> d >> a;

        if (d <= x) {
            int ind = (x - d) / l + 1;
            PQ[ind].push(a);
            maxim = max(maxim, ind);
        }
    }

    int poz = maxim;
    long long sum = 0;
    while (poz) {
        if (PQ[poz].size()) {
            if (PQ[maxim].size()) {
                if (PQ[poz].top() > PQ[maxim].top()) {
                    sum += PQ[poz].top();
                    PQ[poz].pop();
                    if (PQ[poz].top() > PQ[maxim].top()) {
                        maxim = poz;
                    }
                } else {
                    sum += PQ[maxim].top();
                    PQ[maxim].pop();
                    for (int i = poz; i <= maxim; ++i) {
                        if (PQ[i].top() > PQ[maxim].top()) {
                            maxim = i;
                        }
                    }
                }
            } else {
                sum += PQ[poz].top();
                PQ[poz].pop();
                maxim = poz;
            }
        } else {
            if (PQ[maxim].size()) {
                sum += PQ[maxim].top();
                PQ[maxim].pop();
                for (int i = poz; i <= maxim; ++i) {
                    if (PQ[i].size() && PQ[i].top() > PQ[maxim].top()) {
                        maxim = i;
                    }
                }
            }
        }

        poz--;
    }

    fout << sum;
    return 0;
}