#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<pair<int, int>> a;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    a.push_back(make_pair(y, x));
  }
  int c = 1, ans = 0;
  sort(a.begin(), a.end(), greater<pair<int, int>>());
  for (auto p : a) {
    if (c > 0) {
      c += p.first;
      ans += p.second;
    }
    c--;
  }
  cout << ans << endl;
}
