#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000;
pair<int, int> f[maxn];
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.first == b.first) {
    return (a.second > b.second);
  }
  return (a.first > b.first);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> f[i].second >> f[i].first;
  }
  sort(f, f + n, cmp);
  long long L = 1, sum = 0;
  for (int i = 0; i < n; i++) {
    if (L > 0) {
      L--;
      sum += f[i].second;
      L += f[i].first;
    }
  }
  cout << sum << endl;
  return 0;
}
