#include <bits/stdc++.h>
using namespace std;
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second > b.second;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end(), sortinrev);
  stable_sort(v.begin(), v.end(), sortbysecdesc);
  int moves = 1;
  int ans = 0;
  int i = 0;
  while (moves > 0 && i < v.size()) {
    ans += v[i].first;
    moves += v[i].second;
    moves--;
    i++;
  }
  cout << ans << endl;
  return 0;
}
