#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) {
  if (a < b) return b;
  return a;
}
int main() {
  int ct0 = 0;
  int ct1 = 0;
  int n;
  cin >> n;
  int pt = 0;
  int bum[1000];
  int nobum[1000][2];
  for (int i = 0; i < n; ++i) {
    int j, k;
    cin >> j >> k;
    if (k == 0) {
      bum[ct0] = j;
      ++ct0;
    } else {
      nobum[ct1][0] = j;
      nobum[ct1][1] = k;
      ++ct1;
    }
  }
  for (int i = 0; i < ct0 + 10; ++i)
    for (int j = 0; j < ct0 - 1; ++j) {
      if (bum[j] < bum[j + 1]) {
        int q = bum[j];
        bum[j] = bum[j + 1];
        bum[j + 1] = q;
      }
    }
  int extra = 0;
  for (int i = 0; i < ct1; ++i) {
    pt += nobum[i][0];
    extra += nobum[i][1];
  }
  extra -= ct1;
  ++extra;
  for (int i = 0; i < n; ++i) {
    if (i < extra) pt += bum[i];
  }
  cout << pt << endl;
}
