#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> ans;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    ans.push_back(make_pair(b, a));
  }
  sort(ans.rbegin(), ans.rend());
  int cnt = 1, point = 0, idx = 0;
  while (cnt > 0 && idx < n) {
    point += ans[idx].second;
    cnt += ans[idx].first - 1;
    idx++;
  }
  cout << point;
}
