#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v;
vector<int> v1;
map<char, int> m1, m;
bool sortInv(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.second == b.second) {
    return (a.first > b.first);
  } else {
    return (a.second > b.second);
  }
}
int n, i, j, b, a, sum = 1, sum1 = 0;
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a >> b;
    v.push_back(make_pair(a, b));
  }
  sort(v.begin(), v.end(), sortInv);
  i = 0;
  while (sum != 0) {
    if (i == n) break;
    sum1 += v[i].first;
    sum += v[i].second;
    i++;
    sum--;
  }
  cout << sum1;
  return 0;
}
