#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 510
#define INF 1000100
struct Node {
	int x, y;
	friend bool operator <(struct Node a, struct Node b) {
		if (a.y != b.y)
			return a.y > b.y;
		return a.x < b.x;
	}
	friend bool operator ==(struct Node a, struct Node b) {
		return a.x == b.x && a.y == b.y;
	}
};
int dp[N];
struct Node d[N];
void init(int n) {
	for (int i = 0; i < n; i++) {
		dp[i] = -INF;
	}
	dp[n] = 0;
}
int main() {
	setvbuf(stdout,NULL,_IONBF,0);
	int n;
	while (scanf(" %d", &n) == 1) {
		init(n);
		memset(d,-1,sizeof(d));
		for (int i = 0; i < n; i++) {
			scanf(" %d %d", &d[i].x, &d[i].y);
		}
		sort(d, d + n);
		for (int i = 0, j = n - 1; i < n; i++) {
			if(d[i].y<0||d[i].x<0) continue;
			while (j > d[i].y) {
				dp[j] = max(dp[j],dp[j+1]);
				j--;
			}
			int k = 1;
			while (d[i] == d[i + 1] && i < n - 1) {
				k++;
				i++;
			}
			for (int e = 1; e + j <= n && e <= k&&e<=n-d[i].x-d[i].y; e++) {
				dp[j] = max(dp[j + e]+e, dp[j]);
			}
		}
		int ans = 0;
		for (int i = 0; i <= n; i++) {
			ans = max(ans, dp[i]);
		}
		printf("%d\n", ans);
	}
	return 0;
}
/*
 5
 4 0
 3 1
 2 2
 3 1
 4 0
 5
 4 0
 3 1
 2 2
 1 3
 0 4
 5
 0 4
 1 3
 2 0
 2 0
 2 0
 5
 0 4
 1 0
 1 0
 1 0
 1 0
 5
 0 4
 1 1
 1 1
 1 1
 4 0
 5
 0 1
 1 1
 1 1
 1 1
 4 0
 5
 0 4
 1 3
 2 0
 2 1
 2 0
3
0 0
0 0
0 0
 */