#include <bits/stdc++.h>
using namespace std;
const int inf = (int)(1e9) + 100;
const int mod = (int)(1e9) + 7;
const int N = (int)(1e5) + 100;
inline int read() {
  int x;
  cin >> x;
  return x;
}
inline void out(int x) {
  cout << x << ' ';
  return;
}
int cur = 1, n, res = 0;
pair<int, int> p[1200];
bool cmp(pair<int, int> a, pair<int, int> b) {
  if (a.second > b.second) return true;
  if (a.second == b.second && a.first > b.first) return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (p[i].second > p[j].second) {
        pair<int, int> tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
      } else if (p[i].second == p[j].second && p[i].first > p[j].first) {
        pair<int, int> tmp = p[i];
        p[i] = p[j];
        p[j] = tmp;
      }
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (cur <= 0)
      break;
    else {
      cur--;
      cur += p[i].second;
      res += p[i].first;
    }
  }
  cout << res;
  return 0;
}
