#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const double pi = 3.14159265358979323846;
int n;
pair<int, int> a[1000];
bool read() {
  if (!(cin >> n)) return false;
  for (int i = 0; i < int(n); ++i) cin >> a[i].first >> a[i].second;
  return true;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) return a.second > b.second;
  return a.first > b.first;
}
void solve() {
  int sum = 1;
  sort(a, a + n, cmp);
  int l = 0;
  int ans = 0;
  while (l < sum && l < n) {
    ans += a[l].first;
    sum += a[l].second;
    l++;
  }
  cout << ans << endl;
}
int main() {
  while (read()) solve();
  return 0;
}
