#include <bits/stdc++.h>
using namespace std;
int main() {
  int sum, i, ctr, x, y, n, t;
  cin >> n;
  pair<int, int> a[n + 1];
  t = n;
  i = 1;
  while (t--) {
    cin >> x >> y;
    a[i].first = y;
    a[i].second = x;
    i++;
  }
  sort(a + 1, a + n + 1);
  ctr = 1;
  i = n;
  sum = 0;
  while (ctr > 0 && i >= 0) {
    ctr = ctr - 1 + a[i].first;
    sum += a[i].second;
    i--;
  }
  cout << sum;
  return 0;
}
