#include <bits/stdc++.h>
using namespace std;
long long n, i, res, sl;
pair<long long, long long> a[500009];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  sl = 1;
  for (i = 1; i <= n; i++) {
    cin >> a[i].second >> a[i].first;
  }
  sort(a + 1, a + n + 1);
  for (i = n; i >= 1; i--) {
    if (sl == 0) {
      break;
    }
    sl--;
    sl += a[i].first;
    res += a[i].second;
  }
  cout << res << '\n';
}
