int const N=150007;
struct L{
ll t1,t2;
friend bool operator<(L a,L b){
return a.t1 < b.t1 ;
}
}a[N];
bool cmp(L a,L b){
return a.t2 <b.t2 ;
}
priority_queue<l>pq;
int main(){
int n;
read(n);
for(int i=1;i<=n;++i){
read(a[i].t1);read(a[i].t2);
}
sort(a+1,a+n+1,cmp);
pq.push(a[1]);
ll cnt=1,t=a[1].t1 ;
for(int i=2;i<=n;++i){
if(t+a[i].t1 <=a[i].t2 ){
pq.push(a[i]);
t+=a[i].t1 ;
cnt++;
}
else if(a[i].t1 <pq.top().t1 && (t-pq.top().t1+a[i].t1) <=a[i].t2 ){
t-=pq.top().t1;
t+=a[i].t1 ;
pq.pop();
pq.push(a[i]);
}
}
cout << cnt << endl;
return 0;
} </l>