#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, c = 1, ans = 0, i;
  cin >> n;
  pair<int, int> p[n];
  for (i = 0; i < n; i++) cin >> p[i].second >> p[i].first;
  sort(p, p + n);
  for (i = n - 1; i >= 0; i--) {
    c--;
    if (c < 0) break;
    c += p[i].first;
    ans += p[i].second;
  }
  cout << ans;
}
