Cod sursa(job #2974607)

Utilizator IoanMihaiIoan Mihai IoanMihai Data 4 februarie 2023 11:32:27
Problema Lupul Urias si Rau Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("lupu.in");
ofstream fout("lupu.out");
#define ll long long
const int NMAX = 100005;
int n, x, l, dist, lg, nr;
pair<ll, ll> v[NMAX];
multiset<ll> mul;
int main()
{
    fin >> n >> x >> l;
    ll ans = 0;
    for (ll i=1;i<=n;i++){
        fin >> dist >> lg;
        v[i] = {dist, lg};
        if (dist <= x) {
            ans += lg;
        }
    }
    if (x == 0){
        fout << ans << '\n';
        return 0;
    }
    ans = 0;
    sort(v+1, v+n+1);
    ll i = 1, lim = x%l;
    while(lim <= x){
        for ( ;i<=n && v[i].first <= lim;i ++){
            mul.insert(v[i].second);
        }
        if (!mul.empty()){
            multiset<ll>::iterator it = mul.end();
            --it;
            ans += *it;
            mul.erase(it);
        }
        lim += l;
    }
    fout << ans << '\n';
    return 0;
}