#include <bits/stdc++.h>
using namespace std;
int main() {
  int t = 1;
  while (t--) {
    int n, s = 0, c = 0;
    cin >> n;
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++) {
      int x, y;
      cin >> x >> y;
      v.push_back(make_pair(y, x));
    }
    sort(v.begin(), v.end(), greater<pair<int, int> >());
    for (int i = 0; i < n; i++) {
      c += v[i].first;
      if (c < 0) break;
      s += v[i].second;
      c--;
    }
    cout << s << endl;
  }
}
