#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, a, b, i, m = 0, s = 0;
  cin >> n;
  pair<int, int> p[n];
  for (i = 0; i < n; i++) {
    cin >> p[i].second >> p[i].first;
  }
  sort(p, p + n);
  int c = 0, k = 1;
  for (i = n - 1; i >= 0 && k > 0; i--) {
    c += p[i].second;
    k += p[i].first - 1;
  }
  cout << c << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
