#include <bits/stdc++.h>
void sort(int* array, int n) {
  int done = 0;
  while (!done) {
    done = 1;
    for (int i = 0; i < n - 1; i++) {
      if (array[i] < array[i + 1]) {
        int aux = array[i];
        array[i] = array[i + 1];
        array[i + 1] = aux;
        done = 0;
      }
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int* array = (int*)malloc(4 * n);
  int nrOfTimes = 1;
  int sum = 0;
  int ct = 0;
  for (int i = 0, a, b; i < n; i++) {
    scanf("%d %d", &a, &b);
    if (b > 0) {
      sum += a;
      nrOfTimes += b - 1;
    } else {
      array[ct] = a;
      ct++;
    }
  }
  sort(array, ct);
  for (int i = 0; i < nrOfTimes && i < ct; i++) sum += array[i];
  printf("%d", sum);
  return 0;
}
