#include <iostream>
#include <algorithm>
#define MAX 150000
#define ll long long
using namespace std;
struct building
{
ll T1;
ll T2;
}Building[MAX], Heap[MAX];
int cmp(const building & a, const building & b)
{
if(a.T2 != b.T2) return a.T2 < b.T2;
return a.T1 < b.T1;
}
int Heapcmp(const building & a, const building & b)
{
return a.T1 < b.T1;
}
int main()
{
int N, tot = 0;
ll ans = 0;
cin >> N;
for(int i = 1; i <= N; ++i)
cin >> Building[i].T1 >> Building[i].T2;
sort(Building + 1, Building + N + 1, cmp);
for(int i = 1; i <= N; ++i)
{
if(ans + Building[i].T1 <= Building[i].T2)
{
Heap[++tot] = Building[i];
push_heap(Heap + 1, Heap + 1 + tot, Heapcmp);
ans = ans + Building[i].T1;
}
else if(tot != 0)
{
building step = Heap[1];
if(Building[i].T1 >= step.T1) continue;
pop_heap(Heap + 1, Heap + 1 + tot, Heapcmp);
Heap[tot] = Building[i];
push_heap(Heap + 1, Heap + 1 + tot, Heapcmp);
ans = ans + Building[i].T1 - step.T1;
}
}
cout << tot;
return 0;
}