#include <bits/stdc++.h>
using namespace std;
struct node {
  int a, b;
};
bool compare(node p1, node p2) {
  if (p1.b == p2.b)
    return (p1.a > p2.a);
  else
    return p1.b > p2.b;
}
int main() {
  int n;
  cin >> n;
  node p[1000];
  for (int i = 0; i < n; i++) {
    cin >> p[i].a >> p[i].b;
  }
  sort(p, p + n, compare);
  int point = 0, cnt = 1;
  for (int i = 0; i < n; i++) {
    point += p[i].a;
    cnt--;
    cnt += p[i].b;
    if (cnt == 0) break;
  }
  cout << point << endl;
  return 0;
}
