#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b;
  cin >> n;
  pair<int, int> p;
  priority_queue<pair<int, int> > pq;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    pq.push(make_pair(b, a));
  }
  int pts = 0;
  p = pq.top();
  pq.pop();
  int nc = p.first;
  pts += p.second;
  while (nc > 0 and !pq.empty()) {
    nc--;
    p = pq.top();
    pq.pop();
    nc += p.first;
    pts += p.second;
  }
  cout << pts << endl;
  return 0;
}
