#include <bits/stdc++.h>
using namespace std;
int max(int *a, int n, int *b) {
  int max = -1, index = -1;
  for (int i = 0; i < n; i++) {
    if (b[i] == -1)
      continue;
    else if (b[i] > max) {
      max = b[i];
      index = i;
    } else if (b[i] == max) {
      if (a[i] > a[index]) {
        index = i;
      }
    }
  }
  return index;
}
int main() {
  int n, turns = 1, points = 0;
  cin >> n;
  int *a = new int[n], *b = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  while (turns > 0) {
    int index = max(a, n, b);
    if (index == -1) break;
    points += a[index];
    turns += b[index];
    b[index] = -1;
    turns--;
  }
  cout << points;
}
