#include <bits/stdc++.h>
using namespace std;
struct Point {
  int point;
  int add;
};
Point data[1001];
int length;
bool comp(Point a, Point b) {
  if (a.add == b.add)
    return a.point > b.point;
  else
    return a.add > b.add;
}
int main() {
  while (cin >> length) {
    for (int time = 0; time < length; ++time) {
      cin >> data[time].point >> data[time].add;
    }
    sort(data, data + length, comp);
    int point(1);
    int sum(0);
    for (int time = 0; time < length && point; ++time, --point) {
      sum += data[time].point;
      point += data[time].add;
    }
    cout << sum << endl;
  }
  return 0;
}
