#include <bits/stdc++.h>
using namespace std;
long long n;
struct mang {
  long long a, b;
} m[100005];
bool operator<(mang u, mang v) {
  if (u.b == v.b) return u.a < v.a;
  return u.b < v.b;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> m[i].a;
    cin >> m[i].b;
  }
  sort(m, m + n);
  long long tam = 1;
  long long res = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (tam == 0) break;
    res += m[i].a;
    tam += (m[i].b);
    tam -= 1;
  }
  cout << res;
  return 0;
}
