Pagini recente » Cod sursa (job #394682) | Cod sursa (job #2922611) | Cod sursa (job #2373452) | Cod sursa (job #413390) | Cod sursa (job #2032173)
#include <cstdio>
#include <algorithm>
using namespace std;
pair <int,int> v[50001],h[50001];
int elem;
int compar (int a,int b){
int x,y,z,t;
x=h[a].first;
y=h[a].second;
z=h[b].first;
t=h[b].second;
return (x*t>z*y);
}
void adauga (int a,int b){
int p,c;
elem++;
h[elem].first=a;
h[elem].second=b;
c=elem;
p=elem/2;
while (p>0){
if (compar (c,p))
swap (h[c],h[p]);
else break;
c=p;
p/=2;
}
}
void sterge (int poz){
int p,c;
h[poz].first=h[elem].first;
h[poz].second=h[elem].second;
elem--;
p=poz;
c=2*poz;
while (c<=elem){
if (c+1<=elem && h[c+1]>h[c])
c++;
if (compar (c,p))
swap (h[c],h[p]);
else break;
p=c;
c*=2;
}
}
int main()
{
FILE *fin=fopen ("peste.in","r");
FILE *fout=fopen ("peste.out","w");
int n,k,t,s,sol,dr,i;
fscanf (fin,"%d%d%d",&n,&k,&t);
for (i=1;i<=n;i++)
fscanf (fin,"%d%d",&v[i].second,&v[i].first);
sort (v+1,v+n+1);
s=0;
for (i=n;n-i+1<=k;i--)
s+=v[i].second;
adauga (s,v[n].first);
dr=n;
for (;i>0;i--){
s-=v[dr].second;
s+=v[i].second;
dr--;
adauga (s,v[dr].first);
}
sol=0;
while (elem){
if (t-h[1].second>=0){
t-=h[1].second;
sol+=h[1].first;
}
else sterge (1);
}
fprintf (fout,"%d",sol);
return 0;
}