P3045
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll n,k,m,ans;
priority_queue<ll,vector<ll>,greater<ll>> q;
struct Node
{
ll p,c;
}a[50005];
bool cmp1(Node a,Node b)
{
return a.c<b.c;
}
bool cmp2(Node a,Node b)
{
return a.p<b.p;
}
int main()
{
cin>>n>>k>>m;
for(int i=1;i<=n;i++)
{
cin>>a[i].p>>a[i].c;
}
sort(a+1,a+n+1,cmp1);
for(int i=1;i<=k&&m>=a[i].c;i++)
{
m-=a[i].c;
q.push(a[i].p-a[i].c);
ans++;
}
sort(a+k+1,a+n+1,cmp2);
for(int i=k+1;i<=n;i++)
{
if(q.empty())
{
if(m>=a[i].p)
{
m-=a[i].p;
ans++;
}
continue;
}
ll t=q.top();
if(a[i].p-a[i].c>t)
{
if(m<t+a[i].c) continue;
m-=t+a[i].c;
ans++;
q.pop();
q.push(a[i].p-a[i].c);
}
else
{
if(m<a[i].p) continue;
m-=a[i].p;
ans++;
}
}cout<<ans<<endl;
return 0;
}WA19 20 21点,反悔贪心的思路 P3045 P3045 P3045 重要的事情说三遍