1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cstdlib>
5 #include<cmath>
6 #include<algorithm>
7 #include<queue>
8 #include<vector>
9 #define LL long long
10 using namespace std;
11 int n;
12 struct data{int t1,t2;}a[150005];
13 bool cmp(data c1,data c2){return c1.t2<c2.t2;}
14 struct cmp1
15 {
16 bool operator()(data c1,data c2){return c1.t1<c2.t1;}
17 };
18 priority_queue<data,vector<data>,cmp1> q;
19 int main()
20 {
21 scanf("%d",&n);
22 for(int i=1;i<=n;i++) scanf("%d%d",&a[i].t1,&a[i].t2);
23 LL sum=0;
24 int ans=0;
25 sort(a+1,a+n+1,cmp);
26 for(int i=1;i<=n;i++)
27 {
28 if(a[i].t2>=sum+a[i].t1){q.push(a[i]);ans++;sum+=a[i].t1;}
29 else
30 {
31 if(q.empty()) continue;
32 data now=q.top();
33 if(now.t1>a[i].t1)
34 {
35 sum=sum-now.t1+a[i].t1;
36 q.pop();
37 q.push(a[i]);
38 }
39 }
40 }
41 printf("%d",ans);
42 }