#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define MAX 100000000000000007 #define LL long long priority_queue< LL,vector<LL>,greater<LL> > h; LL n,m,k,s,a_xb,b_xb,ans; bool f[100005]; struct price{ LL x,y,z; bool operator <(const price &s)const{ return y<s.y; } } a[100005],b[100005]; bool cmp(struct price x,struct price y){ return x.x<y.x; } int main(){ cin>>n>>m>>k; for (int i=1;i<=n;i++){ cin>>a[i].x>>a[i].y; a[i].z=i; } sort(a+1,a+n+1); for (int i=1;i<=m;i++){ if (s+a[i].y<=k){ s+=a[i].y; h.push(a[i].x-a[i].y); } else{ cout<<i-1; return 0; } } memcpy(b,a,sizeof b); sort(b+m+1,b+n+1,cmp); a_xb=m+1; b_xb=m+1; a[n+1].x=a[n+1].y=MAX; b[n+1].x=b[n+1].y=MAX; ans=m; while (a_xb<=n||b_xb<=n){ if (a[a_xb].y+h.top()<b[b_xb].x){ s+=a[a_xb].y+h.top(); h.pop(); h.push(a[a_xb].x-a[a_xb].y); f[a[a_xb].z]=1; } else{ s+=b[b_xb].x; f[b[b_xb].z]=1; } if (s>k){ cout<<ans; return 0; } else ans++; while (f[a[a_xb].z]&&a_xb<=n) a_xb++; while (f[b[b_xb].z]&&b_xb<=n) b_xb++; } cout<<ans; return 0; }
|