#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
struct Node {
  int a, b;
};
Node card[MAXN];
int cmp(const void* p1, const void* p2) {
  Node v1 = *(Node*)p1;
  Node v2 = *(Node*)p2;
  if (v1.b != v2.b)
    return (v1.b > v2.b ? 1 : -1);
  else
    return (v1.a > v2.a ? 1 : -1);
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 0; i < n; i++) scanf("%d%d", &card[i].a, &card[i].b);
    int cnt = 1;
    int score = 0;
    qsort(card, n, sizeof(Node), cmp);
    for (int i = n - 1; i >= 0; i--) {
      if (card[i].b >= 1) {
        score += card[i].a;
        cnt += card[i].b - 1;
      } else {
        if (cnt > 0)
          score += card[i].a;
        else
          break;
        cnt--;
      }
    }
    printf("%d\n", score);
  }
  return 0;
}
