#include <bits/stdc++.h>
using namespace std;
struct card {
  int pts;
  int count;
};
bool cardcomp(card c1, card c2) {
  if (c1.count > c2.count) {
    return 1;
  } else if (c1.count == c2.count && c1.pts > c2.pts) {
    return 1;
  }
  return 0;
}
int main() {
  int n;
  cin >> n;
  struct card a[n];
  struct card s;
  for (int i = 0; i < n; i++) {
    cin >> s.pts >> s.count;
    a[i] = s;
  }
  sort(a, a + n, cardcomp);
  int sum = 0;
  int turns = 1;
  int i = 0;
  while (i < n && turns > 0) {
    sum += a[i].pts;
    turns += a[i].count - 1;
    i++;
  }
  cout << sum;
  return 0;
}
