#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const long long MAX = 1e3 + 5;
const long long MAX2 = 11;
const int MOD = 1000000000 + 7;
const long long INF = 20000;
const int nr[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int nc[] = {0, 0, 1, -1, 1, -1, 1, -1};
int n, nw, ans;
pair<int, pair<int, int> > x[MAX], y[MAX];
bool vis[MAX];
int main() {
  cout << fixed << setprecision(10);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i].first >> x[i].second.first;
    x[i].second.second = i;
    y[i] = x[i];
    swap(y[i].first, y[i].second.first);
  }
  sort(x + 1, x + 1 + n);
  reverse(x + 1, x + 1 + n);
  sort(y + 1, y + 1 + n);
  reverse(y + 1, y + 1 + n);
  nw = 1;
  for (int i = 1; i <= n; i++) {
    if (y[i].first == 0) break;
    vis[y[i].second.second] = 1;
    ans += y[i].second.first;
    nw += y[i].first - 1;
  }
  for (int i = 1; i <= n; i++) {
    if (nw == 0) break;
    if (vis[x[i].second.second]) continue;
    ans += x[i].first;
    nw--;
  }
  cout << ans << "\n";
  return 0;
}
