#include <bits/stdc++.h>
using namespace std;
int const MAXN = 1e3 + 10;
vector<pair<pair<int, int>, int> > card;
vector<pair<int, int> > ini;
bool mrk[MAXN];
bool cmp(pair<pair<int, int>, int> A, pair<pair<int, int>, int> B) {
  if (A.first.second > B.first.second ||
      (A.first.second == B.first.second && A.first.first > B.first.first))
    return true;
  return false;
}
bool _cmp(pair<int, int> A, pair<int, int> B) {
  if (A.first >= B.first) return true;
  return false;
}
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    int top, bottom;
    scanf("%d %d", &top, &bottom);
    ini.push_back(pair<int, int>(top, i));
    card.push_back(pair<pair<int, int>, int>(pair<int, int>(top, bottom), i));
  }
  sort(ini.begin(), ini.end(), _cmp);
  sort(card.begin(), card.end(), cmp);
  int ans = 0, chance = 0;
  ans = card[0].first.first, chance = card[0].first.second,
  mrk[card[0].second] = true;
  for (int i = 1; i < N; i++) {
    if (!chance) break;
    if (!card[i].first.second && chance) {
      while (chance > 0) {
        int next = -1;
        for (int j = 0; j < N; j++) {
          if (!mrk[ini[j].second]) {
            mrk[ini[j].second] = true;
            next = ini[j].first;
            break;
          }
        }
        if (next == -1) break;
        ans += next;
        chance--;
      }
      break;
    } else
      ans += card[i].first.first, chance += card[i].first.second - 1,
          mrk[card[i].second] = true;
  }
  printf("%d\n", ans);
  return 0;
}
