#include <bits/stdc++.h>
using namespace std;
bool compare(const pair<int, int>& a, const pair<int, int>& b) {
  if (a.second == b.second) return a.first > b.first;
  return a.second > b.second;
}
int main() {
  int x, y, n, counter = 1, point = 0;
  cin >> n;
  vector<pair<int, int> > vec;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    vec.push_back(make_pair(x, y));
  }
  sort(vec.begin(), vec.end(), compare);
  for (int i = 0; i < n; i++) {
    point += vec[i].first;
    counter = counter + vec[i].second - 1;
    if (counter <= 0) break;
  }
  cout << point << endl;
}
