#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, counter = 1, profit = 0;
  cin >> n;
  priority_queue<int> q;
  while (n--) {
    int a, b;
    cin >> a >> b;
    if (b) {
      profit += a;
      counter += b - 1;
    } else {
      q.push(a);
    }
  }
  while (!q.empty() && counter != 0) {
    profit += q.top();
    counter--;
    q.pop();
  }
  cout << profit << endl;
}
