Cod sursa(job #1403908)

Utilizator rangerChihai Mihai ranger Data 27 martie 2015 17:25:49
Problema Lupul Urias si Rau Scor 32
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include<fstream>
#include<algorithm>
#include<set>

using namespace std;
#define Nmax 100003

ifstream cin("lupu.in");
ofstream cout("lupu.out");

int n, i, H, pas, h, RS;
set<int> s;

struct el
{
    int dist, cost;
};
el a[Nmax];
bool cmp(el a, el b)
{
    return a.dist < b.dist;
}
set<int> :: iterator it;
int main()
{
    cin>>n>>H>>pas;

    for (i=1;i<=n;i++)
        cin>>a[i].dist>>a[i].cost;
    sort(a+1,a+n+1,cmp);

    h=H;
    while (h>=a[1].dist) h-=pas;
    h+=pas;


    for (i=1 ; h<=H; h+=pas )
    {
        while (i<=n && a[i].dist<=h) s.insert(a[i].cost),i++;
        if (s.size()) {
            it  = s.end(); it--;
             RS += *it;
            s.erase(it);
        }
    }
    cout<<RS;
    return 0;
}