#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int jalan = 1, n, kartuDepan, kartuBelakang;
  long long poin = 0;
  vector<int> kartu;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> kartuDepan >> kartuBelakang;
    if (kartuBelakang > 0) {
      jalan += kartuBelakang - 1;
      poin += kartuDepan;
    } else
      kartu.push_back(kartuDepan);
  }
  sort(kartu.begin(), kartu.end());
  for (int i = kartu.size() - 1; jalan != 0 && i >= 0; i--) {
    poin += kartu[i];
    jalan--;
  }
  cout << poin << "\n";
}
