#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int main() {
  long long int t, b, m, n, i, j, temp, chance, total_chance, hell, l, r, k,
      ans, c, p, q, val, count, total, sum;
  long long int a[100001];
  pair<long long int, long long int> card[100001];
  vector<long long int> v;
  string s;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> card[i].second >> card[i].first;
  }
  sort(card, card + n);
  chance = card[n - 1].first;
  total_chance = 1 + chance;
  i = n - 2;
  while (chance != 0 && i >= 0) {
    chance += card[i].first;
    total_chance += card[i].first;
    i--;
    chance--;
  }
  ans = 0;
  i = n - 1;
  while (total_chance > 0 && i >= 0) {
    ans += card[i].second;
    total_chance--;
    i--;
  }
  cout << ans << endl;
  return 0;
}
