#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define MS 234567
#define MAX 1037471823
#define Q 103
using namespace std;
int n, now, ans;
struct node
{
int x, y;
bool operator < (const node &k) const { return y < k.y || (y == k.y && x < k.x); }
} m[MS];
int main()
{
priority_queue <int> q;
scanf("%d", &n);
rep(i, 1, n) scanf("%d%d", &m[i].x, &m[i].y);
sort(m+1, m+1+n); now = 0;
rep(i, 1, n) if (now + m[i].x <= m[i].y)
{
q.push(m[i].x); now += m[i].x; ans++;
}
else if (!q.empty() && q.top() > m[i].x)
{
now = now - q.top() + m[i].x;
q.pop(); q.push(m[i].x);
}
printf("%d\n", ans);
return 0;
}
I2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGZzdHJlYW0+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxxdWV1ZT4KCiNkZWZpbmUgcmVwKGksIGwsIHIpIGZvcihpbnQgaSA9IGw7IGkgPD0gcjsgaSsrKQojZGVmaW5lIGRvd24oaSwgbCwgcikgZm9yKGludCBpID0gbDsgaSA+PSByOyBpLS0pCiNkZWZpbmUgTVMgMjM0NTY3CiNkZWZpbmUgTUFYIDEwMzc0NzE4MjMKI2RlZmluZSBRIDEwMwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBuLCBub3csIGFuczsKc3RydWN0IG5vZGUKewoJaW50IHgsIHk7Cglib29sIG9wZXJhdG9yIDwgKGNvbnN0IG5vZGUgJmspIGNvbnN0IHsgcmV0dXJuIHkgPCBrLnkgfHwgKHkgPT0gay55ICYmIHggPCBrLngpOyB9Cn0gbVtNU107Cgppb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