#include <bits/stdc++.h>
using namespace std;
int main() {
  pair<int, int> c[1005];
  int n, p = 0, r = 1, i;
  cin >> n;
  for (i = 0; i < n; i++) cin >> c[i].second >> c[i].first;
  sort(c, c + n);
  for (i = n - 1; i >= 0; i--) {
    p += c[i].second;
    r += (c[i].first - 1);
    if (r <= 0) break;
  }
  cout << p;
  return 0;
}
