#include <bits/stdc++.h>
using namespace std;
bool pairCompare(const pair<int, int>& firstElem,
                 const pair<int, int>& secondElem) {
  if (firstElem.second == secondElem.second)
    return firstElem.first > secondElem.first;
  else
    return firstElem.second > secondElem.second;
}
int main(void) {
  int n;
  cin >> n;
  vector<pair<int, int> > cards;
  cards.resize(n);
  for (int i = 0; i < n; ++i) cin >> cards[i].first >> cards[i].second;
  sort(cards.begin(), cards.end(), pairCompare);
  int pts = 0, turns = 1;
  for (int i = 0; i < n; ++i) {
    if (turns < 1) break;
    pts += cards[i].first;
    turns += cards[i].second - 1;
  }
  cout << pts;
}
