#include <bits/stdc++.h>
using namespace std;
struct card {
  int a;
  int b;
};
bool mycomp(struct card a1, struct card b1) {
  if (a1.b < b1.b)
    return false;
  else if (a1.b > b1.b)
    return true;
  else {
    if (a1.a < b1.a)
      return false;
    else if (a1.a > b1.a)
      return true;
  }
}
int main() {
  int n;
  cin >> n;
  struct card c[n];
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    c[i].a = x;
    c[i].b = y;
  }
  sort(c, c + n, mycomp);
  int cnt = 1, i = 0, sm = 0;
  while (cnt > 0 && i < n) {
    sm += c[i].a;
    --cnt;
    cnt += c[i].b;
    ++i;
  }
  cout << sm;
}
