Pagini recente » Cod sursa (job #962170) | Cod sursa (job #1791865) | Cod sursa (job #2135085) | Cod sursa (job #430780) | Cod sursa (job #848408)
Cod sursa(job #848408)
#include <cstdio>
#include <cstdlib>
#define maxn 100001
FILE*f=fopen("lupu.in","r");
FILE*g=fopen("lupu.out","w");
int A[maxn],n,x,l,d,T[maxn],heap[maxn],Tmax,indheap;
long long s;
int max(int a,int b)
{
if (a>b)
return a;
return b;
}
void swap(int &a,int &b)
{
int aux;
aux=a;
a=b;
b=aux;
}
void up_heap(int x)
{
if (x==1)
return;
if (heap[x/2]<heap[x])
{
swap(heap[x/2],heap[x]);
up_heap(x/2);
}
}
void down_heap(int x)
{
int m=x;
if ((2*x<=indheap) && (heap[2*x]>heap[x]))
m=2*x;
if ((2*x+1<=indheap) && (heap[2*x+1]>heap[x]))
m=2*x+1;
if (m!=x)
{
swap(heap[m],heap[x]);
down_heap(m);
}
}
int main()
{
int k,x,l,i,j;
fscanf(f,"%d%d%d",&n,&x,&l);
s=0;
for (i=1;i<=n;i++)
{
fscanf(f,"%d%d",&d,&A[i]);
T[i]=(x-d)/l+1;
Tmax=max(T[i],Tmax);
}
indheap=0;
for (i=Tmax;i>0;i--)
{
j=n;
while (j>0)
{
if (T[j]==i)
{ indheap++;
heap[indheap]=A[j];
up_heap(indheap);
}
j--;
}
if (indheap!=0)
{
s+=heap[1];
swap(heap[1],heap[indheap]);
indheap--;
down_heap(1);
}
}
fprintf(g,"%lld",s);
return 0;
}