#include <bits/stdc++.h>
using namespace std;
const int INF = 2123456789;
int a, b, c;
int n, m, k;
int x, y;
string s;
vector<int> cards;
int main() {
  cin >> n;
  int ans = 0;
  int moves = 1;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    if (b > 0) {
      moves += b - 1;
      ans += a;
    } else {
      cards.push_back(a);
    }
  }
  sort(cards.begin(), cards.end(), greater<int>());
  for (vector<int>::iterator it = cards.begin(); it != cards.end(); it++) {
    moves--;
    ans += *it;
    if (!moves) break;
  }
  cout << ans << endl;
  return 0;
}
