#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int turns = 1;
  int points = 0;
  vector<int> l;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    if (b == 0) {
      l.push_back(a);
    } else {
      turns += b - 1;
      points += a;
    }
  }
  sort(l.begin(), l.end(), greater<int>());
  for (int i = 0; i < turns; i++) {
    if (i >= (int)l.size()) break;
    if (i < (int)l.size()) {
      points += l[i];
    }
  }
  cout << points << "\n";
}
