#include <bits/stdc++.h>
using namespace std;
bool com(int i, int j) { return i > j; }
int main() {
  int n, a, b, i, k, sum, counter;
  int num[1000];
  while (cin >> n) {
    counter = 1;
    sum = 0;
    k = 0;
    for (i = 0; i < n; ++i) {
      cin >> a >> b;
      if (b) {
        counter += b;
        --counter;
        sum += a;
      } else {
        num[k] = a;
        ++k;
      }
    }
    sort(num, num + k, com);
    i = 0;
    while (counter > 0 && i < k) {
      sum += num[i];
      ++i;
      --counter;
    }
    cout << sum << endl;
  }
}
