#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int i, j, temp;
  vector<pair<int, int> > v;
  for (i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back(make_pair(b, a));
  }
  sort(v.rbegin(), v.rend());
  int ans = 0, count = 1;
  for (i = 0; i < n; i++) {
    int b = v[i].first;
    int a = v[i].second;
    ans += a;
    count += b;
    count--;
    if (count == 0) break;
  }
  cout << ans << endl;
}
