#include <bits/stdc++.h>
using namespace std;
typedef struct Node {
  int a;
  int b;
};
bool compare(Node node1, Node node2) {
  if (node1.b == node2.b) {
    return node1.a > node2.a;
  } else {
    return node1.b > node2.b;
  }
}
int main() {
  int N;
  cin >> N;
  vector<Node> Arr(N);
  Node node;
  long Total = 0;
  int count = 1;
  int i = 0;
  while (i < N) {
    cin >> Arr[i].a >> Arr[i].b;
    i++;
  }
  sort(Arr.begin(), Arr.end(), compare);
  i = 0;
  while (count != 0 && i < N) {
    count--;
    Total += Arr[i].a;
    count += Arr[i].b;
    i++;
  }
  cout << Total << endl;
  return 0;
}
