#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
vector<pair<int, int> > v;
int main() {
  ios_base::sync_with_stdio(0);
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    v.push_back(make_pair(b, a));
  }
  sort(v.rbegin(), v.rend());
  int cnt = 1, res = 0;
  for (int i = 0; i < n; ++i) {
    if (!cnt) break;
    --cnt;
    res += v[i].second;
    cnt += v[i].first;
  }
  cout << res << "\n";
  return 0;
}
