#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, cnt = 1, point = 0;
  vector<int> x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    if (b == 0)
      x.push_back(a);
    else {
      cnt += (b - 1);
      point += a;
    }
  }
  sort(x.begin(), x.end());
  for (int j = x.size() - 1; j >= 0 && cnt > 0; j--, cnt--) {
    point += x[j];
  }
  cout << point << endl;
  return 0;
}
