#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int cnt = 1;
  int a, b;
  vector<pair<int, int>> cards(n);
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    cards[i] = make_pair(b, a);
  }
  int points = 0;
  int i = n - 1;
  sort(cards.begin(), cards.end());
  while (cnt != 0 && i != -1) {
    points += cards[i].second;
    cnt += cards[i].first;
    i--;
    cnt--;
  }
  cout << points;
  return 0;
}
