#include <bits/stdc++.h>
using namespace std;
int dfs_level(int start, int n, int x, vector<vector<int>> gph) {
  vector<int> vis(n + 1, 0);
  vector<int> level(n + 1);
  level[1] = 1;
  queue<int> q;
  q.push(start);
  vis[1] = 1;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (int i = 0; i < gph[p].size(); i++) {
      if (!vis[gph[p][i]]) {
        q.push(gph[p][i]);
        level[gph[p][i]] = level[p] + 1;
        vis[gph[p][i]] = 1;
      }
    }
  }
  return count(level.begin(), level.end(), x);
}
void dfs(int start, vector<int>& vis, vector<vector<int>>& gph) {
  vis[start] = 1;
  for (int i = 0; i < gph[start].size(); i++) {
    if (!vis[gph[start][i]]) {
      dfs(gph[start][i], vis, gph);
    }
  }
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    v.push_back({b, a});
  }
  sort(v.rbegin(), v.rend());
  int score = 0;
  int cnt = 1;
  for (int i = 0; i < n; i++) {
    if (cnt > 0) {
      cnt--;
      cnt += v[i].first;
      score += v[i].second;
    }
  }
  cout << score << endl;
}
