#include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 100;
int n;
pair<int, int> a[maxN];
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
  sort(a, a + n, greater<pair<int, int> >());
  int cnt = 1, res = 0;
  for (int i = 0; i < n; i++)
    if (a[i].second > 0) cnt += a[i].second - 1, res += a[i].first;
  for (int i = 0; cnt && i < n; i++)
    if (a[i].second == 0) {
      res += a[i].first;
      cnt--;
    }
  cout << res << endl;
  return 0;
}
