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