#include <bits/stdc++.h>
using namespace std;
pair<int, int> arr[1212];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N, ans = 0, c = 1;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> arr[i].second >> arr[i].first;
  }
  sort(arr + 1, arr + N + 1);
  int idx = 1;
  while (arr[idx].first == 0 && idx <= N) idx++;
  for (int i = idx; i <= N; i++) {
    ans += arr[i].second;
    c += arr[i].first;
    c--;
  }
  for (int i = idx - 1; i > 0; i--) {
    if (!c) break;
    ans += arr[i].second;
    c--;
  }
  cout << ans << endl;
}
