#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  int a, b;
  vector<pair<int, int>> p(n);
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    p[i] = {b, a};
  }
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  int ch = 1;
  int sum = 0, ind = 0;
  while (ch > 0 && ind < n) {
    ch--;
    ch += p[ind].first;
    sum += p[ind].second;
    ind++;
  }
  cout << sum << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  while (T--) {
    solve();
  }
}
