#include <bits/stdc++.h>
using namespace std;
bool myfunc(pair<int, int> p1, pair<int, int> p2) {
  return (p1.second > p2.second) ||
         (p1.second == p2.second && p1.first > p2.first);
}
int main() {
  int n, a, b, c = 1, s = 0;
  vector<pair<int, int> > v;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end(), myfunc);
  for (int i = 0; i < n; ++i) {
    if (c == 0) break;
    s += v[i].first;
    c += (-1 + v[i].second);
  }
  cout << s << endl;
  return 0;
}
