#include <bits/stdc++.h>
using namespace std;
int min(int i, int j) { return i < j ? i : j; }
int main() {
  int n;
  cin >> n;
  vector<int> vi;
  int ncards = 1;
  int npoints = 0;
  for (int i = 0; i < n; i++) {
    int p, c;
    cin >> p >> c;
    if (c != 0) {
      npoints += p;
      ncards += (c - 1);
    } else {
      vi.push_back(p);
    }
  }
  sort(vi.begin(), vi.end());
  reverse(vi.begin(), vi.end());
  for (int i = min(ncards, (int)vi.size()); i > 0; --i) {
    npoints += vi[i - 1];
  }
  cout << npoints << endl;
  cin >> n;
}
