1 #include<queue>
2 #include<algorithm>
3 #include<cstdio>
4 #include<cstdlib>
5 using namespace std;
6
7 #define maxn (150010)
8 int n,ans;
9 struct node
10 {
11 int t1,t2;
12 friend inline bool operator <(node a,node b) { return a.t2 < b.t2; }
13 inline void read() { scanf("%d %d",&t1,&t2); }
14 }build[maxn];
15 priority_queue <int> heap;
16
17 int main()
18 {
19 freopen("1029.in","r",stdin);
20 freopen("1029.out","w",stdout);
21 scanf("%d",&n);
22 for (int i = 1;i <= n;++i) build[i].read();
23 sort(build+1,build+n+1);
24 int now = 0;
25 for (int i = 1;i <= n;++i)
26 {
27 if (now + build[i].t1 <= build[i].t2)
28 {
29 ++ans; heap.push(build[i].t1);
30 now += build[i].t1;
31 }
32 else if (heap.top() > build[i].t1&&now - heap.top() + build[i].t1 <= build[i].t2)
33 {
34 now = now - heap.top() + build[i].t1;
35 heap.pop(); heap.push(build[i].t1);
36 }
37 }
38 printf("%d",ans);
39 fclose(stdin); fclose(stdout);
40 return 0;
41 }