1 /*
2 贪心:按照能选的个数和点数降序排序,当条件不符合就break,水题啊!
3 */
4 #include <cstdio>
5 #include <algorithm>
6 #include <cstring>
7 #include <cmath>
8 using namespace std;
9
10 const int MAXN = 1e3 + 10;
11 const int INF = 0x3f3f3f3f;
12 struct Card
13 {
14 int a, b;
15 }card[MAXN];
16
17 bool cmp(Card x, Card y)
18 {
19 if (x.b == y.b) return x.a > y.a;
20 return x.b > y.b;
21 }
22
23 int main(void) //Codeforces Round #109 (Div. 2) B. Combination
24 {
25 // freopen ("B.in", "r", stdin);
26
27 int n;
28 while (scanf ("%d", &n) == 1)
29 {
30 for (int i=1; i<=n; ++i)
31 {
32 scanf ("%d%d", &card[i].a, &card[i].b);
33 }
34 sort (card+1, card+1+n, cmp);
35
36 int ans = 0; int res = 1; int cnt = 1;
37 for (int i=1; i<=n; ++i)
38 {
39 res += card[i].b - 1; ans += card[i].a;
40 cnt = cnt - 1 + card[i].b;
41 if (cnt <= 0) break;
42 if (res <= 0) break;
43 }
44
45 printf ("%d\n", ans);
46 }
47
48 return 0;
49 }