P3045 [USACO12FEB]Cow Coupons G
include
include
include
include
include
include
include
define int long long
struct co{
int p;
int c;
int d;
}c[500005];
int n,k,m;
struct pp{
int x;
int id;
friend bool operator <(pp x,pp y){
return x.x>y.x;
}
};
std::priority_queue
bool cmp(co x,co y){
return x.c<y.c;
}
bool cmpp(co x,co y){
return x.p<y.p;
}
using namespace std;
signed main(){
scanf("%d%d%d",&n,&k,&m);
for(int i=1;i<=n;++i){
scanf("%d%d",&c[i].p,&c[i].c);
c[i].d=c[i].p-c[i].c;
}
sort(c+1,c+n+1,cmp);
for(int i=1;i<=k;++i){
if(m<c[i].c){
cout<<i-1;
return 0;
}
m-=c[i].c;
p.push((pp){
c[i].d,i
});
}
for(int i=k+1;i<=n;++i){
ppp.push((pp) {
c[i].p,i
});
}
int ans=k;
while(m&&ans!=n){
pp x=p.top();
p.pop();
pp y=ppp.top();
ppp.pop();
if(c[x.id].c+c[y.id].p>=c[y.id].c+c[x.id].p){
if(m<c[x.id].d+c[y.id].c) break;
m-=c[x.id].d+c[y.id].c;
p.push((pp){
c[y.id].d,y.id
});
ans++;
}else{
if(m<c[y.id].p) break;
m-=c[y.id].p;
p.push((pp){
c[x.id].d,x.id
});
ans++;
}
}
cout<<ans<<endl;
return 0;
}