#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  long long s = 0;
  int a[n], b = 1, d;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> d;
    if (d > 0) {
      if (b > 0) b--;
      b += d;
      s += a[i];
      a[i] = 0;
    }
  }
  sort(a, a + n, greater<int>());
  if (b == 0)
    cout << a[0];
  else {
    for (int i = 0; i < b; i++) {
      if (!a[i]) break;
      s += a[i];
    }
    cout << s;
  }
}
