Cod sursa(job #2292556)

Utilizator MrRobotMrRobot MrRobot Data 29 noiembrie 2018 17:56:49
Problema Lupul Urias si Rau Scor 32
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

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

int main()
{
    int n, x, L;

    vector <pair<int, int>> oi;
    fin>>n>>x>>L;
    int i;
    for(i=1;i<=n; i++)
    {
        int d, a;
        fin>>d>>a;
        oi.push_back({d,a});
    }
    sort(oi.begin(), oi.end());
    multiset <int> heap;
    int dr = x%L; //interval distanta
    long long s=0;

    for(int index=0; dr<=x; dr+=L) //st = dr+1
    {
        if(oi[index].first > dr)
            dr = (oi[index].first-x%L)/L*(L+1)+x%L;

        while(dr-L+1<=oi[index].first && oi[index].first<=dr && index<n)
        {
            heap.insert(-oi[index].second);
            index++;
        }
        if(!heap.empty())
        {
            s-=*heap.begin();
            heap.erase(heap.begin());
        }
    }

    fout<<s;
}