#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> p1, pair<int, int> p2) {
  if (p1.second > p2.second) {
    return true;
  } else if (p1.second == p2.second) {
    return p1.first > p2.first;
  }
  return false;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  sort(v.begin(), v.end(), compare);
  int pt = 0;
  int ct = 1;
  for (int i = 0; i < n && ct; i++) {
    pt += v[i].first;
    ct = ct - 1 + v[i].second;
  }
  cout << pt << endl;
  return 0;
}
