Cod sursa(job #2964750)

Utilizator Luca_CristianZamfir Luca-Cristian Luca_Cristian Data 13 ianuarie 2023 20:46:01
Problema Lupul Urias si Rau Scor 32
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <queue>
#include <algorithm>

using namespace std;


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

#define int long long
const int Nmax = 1e5 + 5;
pair <int,int> vsheep[Nmax];

bool cmp(pair <int, int> a, pair <int, int> b)
{
    return a.first > b.first;
}

signed main()
{
    int n, x, l, i;

    fin >> n >> x >> l;
    for(i = 1; i <= n; i++)
    {
        fin >> vsheep[i].first >> vsheep[i].second;

        vsheep[i].first = (x - vsheep[i].first) / l;
    }

    sort(vsheep + 1, vsheep + n + 1, cmp);

    /*for(i = 1; i <= n; i++)
        fout << vsheep[i].first << " " << vsheep[i].second << '\n';*/

    int timp = vsheep[1].first, ans = 0;

    priority_queue <int> pq;
    i = 1;
    while(i <= n && timp >= 0)
    {
        int timpc = vsheep[i].first;
        if(timpc >= 0)
        {
            while(i <= n && vsheep[i].first >= timpc)
            {
                pq.push(vsheep[i].second);
                i++;
            }

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

            timp--;
        }
    }

    fout << ans;
    return 0;
}