#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  int n, c = 1, acum = 0;
  cin >> n;
  vector<pair<int, int> > a(n);
  for (int i = 0; i < n; ++i) cin >> a[i].second >> a[i].first;
  sort(a.rbegin(), a.rend());
  for (int i = 0; i < min(n, c); ++i) {
    acum += a[i].second;
    c += a[i].first;
  }
  cout << acum << endl;
  return EXIT_SUCCESS;
}
