Cod sursa(job #2974746)

Utilizator rastervcrastervc rastervc Data 4 februarie 2023 15:26:28
Problema Lupul Urias si Rau Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <queue>
#include <algorithm>
#include <queue>

using namespace std;

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

constexpr size_t LIM = 1e5 + 5;

int N, X, L, i, D, A;
pair<int, int> sheep[LIM];
priority_queue<int> pq;
long long ans;

int main() {
    fin >> N >> X >> L;
    for (i = 1; i <= N; ++i) {
        fin >> D >> A;
        sheep[i] = { D, A }; 
        if (D <= X) ans += A;
    }

    if (X == 0) {
        fout << ans;
        goto program_end;
    }

    sort(sheep + 1, sheep + N + 1);

    ans = 0;
    for (int j = X % L, i = 1; j <= X; j += L) {
        int mx = 0;
        for (; i <= N && sheep[i].first <= j; ++i)
            pq.push(sheep[i].second);
        if (!pq.empty()) {
            ans += pq.top();
            pq.pop();
        }
    }

    fout << ans;

program_end:
    fin.close();
    fout.close();
    return 0;
}