#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
bool com(pair<int, int> p1, pair<int, int> p2) {
  if (p1.second < p2.second) return 1;
  if (p1.second > p2.second) return 0;
  if (p1.first < p2.first) return 1;
  if (p1.first > p2.first) return 0;
  return 0;
}
int main() {
  int n, j = 0;
  long long ans = 0, c = 1;
  scanf("%d", &n);
  vector<pair<int, int>> v(n);
  for (int i = 0; i < n; i++) scanf("%d%d", &v[i].first, &v[i].second);
  sort(((v).begin()), ((v).end()), com);
  reverse(((v).begin()), ((v).end()));
  while (j < n && c--) ans += v[j].first, c += v[j++].second;
  printf("%d\n", ans);
  return 0;
}
