#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
void solve() {
  int n;
  cin >> n;
  vector<int> v;
  int a, b;
  int score = 0;
  int counter = 1;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    if (b > 0) {
      counter = counter - 1 + b;
      score += a;
    } else
      v.push_back(a);
  }
  sort(v.begin(), v.end(), greater<int>());
  for (auto i : v) {
    score += i;
    counter--;
    if (counter == 0) {
      cout << score;
      return;
    }
  }
  cout << score;
  return;
}
