贪心












#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 150010;
struct Node{
    int cost,end;
    bool operator < (const Node & a) const{
        return cost < a.cost;
    }
}a[N];
bool cmp(Node a,Node b){
    return a.end < b.end;
}
int main(){
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d%d",&a[i].cost,&a[i].end);
    }
    sort(a+1,a+1+n,cmp);
    int now = 0;
    priority_queue<int> q;
    for(int i = 1;i <= n;i++){
        if(now + a[i].cost <= a[i].end){
            now += a[i].cost;
            q.push(a[i].cost);
        }else if(q.empty() || q.top() <= a[i].cost){
            continue;
        }else{
            now -= q.top();q.pop();
            now += a[i].cost;
            q.push(a[i].cost);
        }
    }
    printf("%d\n",q.size());
    return 0;
}