Cod sursa(job #2286725)

Utilizator TooHappyMarchitan Teodor TooHappy Data 20 noiembrie 2018 17:40:55
Problema Lupul Urias si Rau Scor 4
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
 
using namespace std;
 
ifstream in("lupu.in");
ofstream out("lupu.out");

int main() {
    ios::sync_with_stdio(false); in.tie(0); out.tie(0);
    
    int n, x ,l; in >> n >> x >> l;

    vector< pair< int, int > > oi(n);
    for(auto &it: oi) in >> it.first >> it.second;

    sort(oi.begin(), oi.end(), [&](const pair< int, int > &a, const pair< int, int > &b) {
        return a.first > b.first;
    });

    int ans = 0;
    int lastDist = x - l;

    priority_queue< int > pq;
    for(int i = 0; i < n; ++i) {
        if(oi[i].first <= x && oi[i].first > lastDist) {
            pq.push(oi[i].second);
        } else {
            ans += pq.top();
            x = lastDist;
            lastDist -= l;

            while(!pq.empty()) {
                pq.pop();
            }
            --i;
        }
    }

    if(!pq.empty()) {
        ans += pq.top();
    }

    out << ans << "\n";

    in.close(); out.close();
 
    return 0;
}