Cod sursa(job #1318973)

Utilizator dragangabrielDragan Andrei Gabriel dragangabriel Data 16 ianuarie 2015 15:38:39
Problema Lupul Urias si Rau Scor 100
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2014, Anul I Marime 0.74 kb
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

int n,x,l,i,d,a,m,t;
long long sol,add;
struct OI {int dist,y;} p[100005];
priority_queue<int> heap;

bool cmp(OI A,OI B)
{
    return A.dist<B.dist;
}

int main()
{
    freopen("lupu.in","r",stdin);
    freopen("lupu.out","w",stdout);
    scanf("%d %d %d", &n, &x, &l);
    for(i = 1;i <= n;i++)
    {
        scanf("%d %d",&d,&a);
        if(d <= x) p[++m].dist = (x-d) / l+1, p[m].y=a;
    }
    n=m;
    sort(p+1, p+n+1, cmp);
    for(i = n, t = p[n].dist; t ; t--)
    {
        while(i && p[i].dist == t) heap.push(p[i].y), i--;
        if(!heap.empty())
	{
		sol+=heap.top();
		heap.pop();
	}
    }
    printf("%lld\n",sol);
    return 0;
}