Cod sursa(job #1050614)

Utilizator j.loves_rockJessica Joanne Patrascu j.loves_rock Data 8 decembrie 2013 20:15:05
Problema Lupul Urias si Rau Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <algorithm>
#include <queue>
#define NMax 100010

using namespace std;

int n, X, L;
pair <int, int> a[NMax];
long long answer;

void Read()
{
    ifstream f ("lupu.in");
    f>>n>>X>>L;
    for (int i = 1; i<=n; ++i)
    {
        f>>a[i].first>>a[i].second;
        a[i].first = (X - a[i].first) / L + 1;
    }
    f.close();
}

void Solve()
{
    sort (a+1, a+n+1);
    priority_queue <int> Q;
    int timp, i;
    for (i = n, timp = a[n].first; timp > 0; --timp)
    {
        for (; i && a[i].first == timp; Q.push(a[i].second), --i);
        if (!Q.empty())
        {
            answer += Q.top();
            Q.pop();
        }
        else
            if (i)
                timp = a[i].first+1;
            else
                return ;
    }
}

void Write()
{
    ofstream g ("lupu.out");
    g<<answer<<"\n";
    g.close();
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}