#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, potezi = 1, bodovi = 0;
  scanf("%d", &n);
  pair<int, int> a[n];
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i].second, &a[i].first);
  }
  sort(a, a + n);
  for (int i = n - 1; i >= 0; i--) {
    if (!potezi) break;
    potezi += a[i].first - 1;
    bodovi += a[i].second;
  }
  printf("%d\n", bodovi);
  return 0;
}
