#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s = 1, s1 = 0, cnt;
  cin >> n;
  int a[1010], b[1010];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    if (b[i] != 0) {
      s += b[i] - 1;
      s1 += a[i];
      a[i] = 0;
      ++cnt;
    }
  }
  sort(a, a + n);
  reverse(a, a + n);
  for (int i = 0; i < s && i < n - cnt; i++) s1 += a[i];
  cout << s1;
}
