#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int l, r;
    cin >> r >> l;
    v.push_back(make_pair(l, r));
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  int len = v.size();
  long long k = 1, sum = 0, i = 0;
  while (k--) {
    sum += v[i].second;
    k += v[i].first;
    i++;
    if (i == len) break;
  }
  cout << sum << endl;
  return 0;
}
