#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.second > b.second)
    return 1;
  else if (a.first > b.first && a.second == b.second)
    return 1;
  return false;
}
int main() {
  int n;
  cin >> n;
  pair<int, int> c[10000];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> c[i].first >> c[i].second;
  }
  sort(c, c + n, cmp);
  int p = 1;
  for (int i = 0; i < n && i < p; i++) {
    p += c[i].second;
    cnt += c[i].first;
  }
  cout << cnt;
  return 0;
}
