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