#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  pair<int, int> a[1111];
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
  sort(a, a + n, [](pair<int, int> &f, pair<int, int> &s) {
    return (f.second > s.second) || (f.second == s.second && f.first > s.first);
  });
  int can = 1, ans = 0;
  for (int i = 0; i < n; ++i) {
    if (can == 1 && a[i].second == 0) {
      ans += a[i].first;
      break;
    }
    ans += a[i].first;
    can += a[i].second - 1;
  }
  cout << ans << "\n";
  cin.sync();
  cin.get();
  return 0;
}
