1 var
2 i,j,k,m,n,head:longint;
3 l:int64;
4 a,lef,rig,fix,b,c,d:array[0..200000] of longint;
5 function min(x,y:longint):longint;inline;
6 begin
7 if x<y then min:=x else min:=y;
8 end;
9 function max(x,y:longint):longint;inline;
10 begin
11 if x>y then max:=x else max:=y;
12 end;
13 procedure swap(var x,y:longint);inline;
14 var z:longint;
15 begin
16 z:=x;x:=y;y:=z;
17 end;
18 procedure sort(l,r:longint);inline;
19 var i,j,x,y:longint;
20 begin
21 i:=l;j:=r;
22 x:=c[(i+j) div 2];
23 repeat
24 while c[i]<x do inc(i);
25 while c[j]>x do dec(j);
26 if i<=j then
27 begin
28 swap(c[i],c[j]);
29 swap(d[i],d[j]);
30 inc(i);dec(j);
31 end;
32 until i>j;
33 if l<j then sort(l,j);
34 if i<r then sort(i,r);
35 end;
36 procedure merge(var x,y:longint);inline;
37 begin
38 if x=0 then swap(x,y);
39 if y=0 then exit;
40 if a[x]<a[y] then swap(x,y);
41 merge(rig[x],y);
42 fix[x]:=max(fix[lef[x]],fix[rig[x]])+1;
43 if fix[lef[x]]<fix[rig[x]] then swap(lef[x],rig[x]);
44 end;
45 procedure cuthead(var head:longint);inline;
46 begin
47 if head=0 then exit;
48 merge(lef[head],rig[head]);
49 head:=lef[head];
50 end;
51 begin
52 readln(n);
53 for i:=1 to n do readln(d[i],c[i]);
54 sort(1,n);l:=0;
55 head:=0;m:=0;k:=0;
56 fillchar(a,sizeof(a),0);
57 fillchar(lef,sizeof(lef),0);
58 fillchar(rig,sizeof(rig),0);
59 fillchar(fix,sizeof(fix),0);
60 for i:=1 to n do
61 begin
62 if int64(l+int64(d[i]))<=int64(c[i]) then
63 begin
64 l:=int64(l+int64(d[i]));inc(k);
65 inc(m);a[m]:=d[i];
66 j:=m;merge(head,j);
67 end
68 else
69 begin
70 if (int64(d[i])<int64(a[head])) then
71 begin
72 l:=int64(l+int64(d[i])-int64(a[head]));
73 inc(m);a[m]:=d[i];
74 cuthead(head);j:=m;
75 merge(head,j);
76 end;
77 end;
78 end;
79 writeln(k);
80 readln;
81 end.
82