#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
  if (x.second == y.second) return x.first > y.first;
  return x.second > y.second;
}
int32_t main() {
  long long n;
  cin >> n;
  pair<long long, long long>* a = new pair<long long, long long>[n];
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    a[i] = {x, y};
  }
  sort(a, a + n, cmp);
  long long ans = 0;
  long long posVal = 1;
  for (long long i = 0; i < n; i++) {
    if (posVal == 0) break;
    ans += a[i].first;
    posVal += a[i].second;
    posVal--;
  }
  cout << ans << endl;
}
