#include <bits/stdc++.h>
using namespace std;
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.first > b.first);
}
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second > b.second;
}
int main() {
  int n = 0, cnt = 1, ans = 0;
  cin >> n;
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) cin >> v[i].second >> v[i].first;
  sort(v.begin(), v.end(), sortinrev);
  for (int i = 0; i < n; i++) {
    if (v[i].first == 0)
      break;
    else
      cnt += v[i].first - 1, ans += v[i].second, v[i].second = -1;
  }
  sort(v.begin(), v.end(), sortbysecdesc);
  for (int i = 0; i < n && cnt > 0; i++) {
    if (v[i].second == -1) break;
    ans += v[i].second, cnt--;
  }
  cout << ans << endl;
}
