#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  multiset<int, greater<int>> r;
  long long count = 0, pt = 0;
  for (; t > 0; t--) {
    int a, b;
    cin >> a >> b;
    if (b != 0) {
      pt += a;
      if (count == 0)
        count += b;
      else
        count += (b - 1);
    } else
      r.insert(a);
  }
  multiset<int, greater<int>>::iterator it = r.begin();
  if (count == 0) {
    pt = *(r.begin());
  } else {
    while (count > 0 && it != r.end()) {
      pt += *it;
      it++;
      count--;
    }
  }
  cout << pt << endl;
  return 0;
}
