#include <bits/stdc++.h>
using namespace std;
int min(int a, int b) { return (a <= b) ? a : b; }
int main() {
  int n;
  scanf("%d", &n);
  pair<int, int> p[1000];
  for (int i = 0; i < n; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    p[i].first = b;
    p[i].second = a;
  }
  sort(p, p + n);
  bool token[1000];
  memset(token, false, sizeof(token));
  int cnt = 1, points = 0, N = n, base = n;
  while (cnt && N) {
    --N;
    if (cnt == 1) {
      for (base = base - 1; base >= 0 && token[base]; --base)
        ;
      token[base] = true;
      points += p[base].second;
      cnt = cnt + p[base].first - 1;
      continue;
    }
    int maximum = -1, idx;
    for (int j = 0; j < base; ++j)
      if (!token[j] && p[j].second > maximum) {
        maximum = p[j].second;
        idx = j;
      }
    token[idx] = true;
    points += p[idx].second;
    cnt = cnt + p[idx].first - 1;
  }
  printf("%d\n", points);
  return 0;
}
