#include <bits/stdc++.h>
using namespace std;
double PI = 3.14159265358979;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  vector<pair<long long int, long long int> > v;
  for (int i = 0; i < n; i++) {
    long long int a, b;
    cin >> a >> b;
    v.push_back({b, a});
  }
  sort(v.begin(), v.end());
  long long int ans = 0, cnt = 1, i = n - 1;
  while (cnt > 0 && i >= 0) {
    ans += v[i].second;
    cnt--;
    cnt += v[i].first;
    i--;
  }
  cout << ans << "\n";
  return 0;
}
