#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int n;
pair<int, int> a[N];
vector<int> vec;
int main() {
  cin >> n;
  int ans = 0;
  int k = 0;
  bool bol = true;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].second == 0)
      vec.push_back(a[i].first);
    else {
      bol = false;
      ans += a[i].first;
      k += a[i].second - 1;
    }
  }
  k++;
  sort(vec.begin(), vec.end());
  reverse(vec.begin(), vec.end());
  for (int i = 0; i < min(int(vec.size()), k); i++) ans += vec[i];
  cout << ans;
}
