#include <bits/stdc++.h>
using namespace std;
int p[1005];
int main() {
  int ans = 0, n, x, y, r = 1, j = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    if (y == 0)
      p[j++] = x;
    else
      ans += x, r += y - 1;
  }
  sort(p, p + j);
  for (int i = j - 1; i >= 0 && r > 0; i--) ans += p[i], r--;
  printf("%d\n", ans);
}
