#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int mod = 1000000007;
const int maxn = 2000010;
long long int modPower(int a, long long int n) {
  if (n == 0) return 1;
  long long int u = modPower(a, n / 2);
  u = (u * u);
  if (n & 1) u = (u * a);
  return u;
}
bool is_prime(long long int n) {
  long long int i;
  if (n <= 1) return false;
  if (n <= 3)
    return true;
  else if (n % 2 == 0 || n % 3 == 0)
    return false;
  else {
    for (i = 5; i * i <= n; i += 6) {
      if (n % i == 0 || n % (i + 2) == 0) return false;
    }
  }
  return true;
}
bool base(int n, int x) {
  if (n % x == 0)
    return false;
  else if (n % x != 0 && ((n /= x) % x) == 0)
    return true;
  else
    return false;
}
int countSubStr(string str, int n, char x) {
  int res = 0, count = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == x) {
      res += ((count + 1) * (n - i));
      count = 0;
    } else
      count++;
  }
  return res;
}
int phi[10001];
char text[100001], p[10001];
void preFun(char patt[]) {
  int l = 0;
  int r = 1;
  phi[l] = 0;
  while (r < strlen(patt)) {
    if (patt[l] == patt[r]) {
      phi[r] = l + 1;
      l++;
      r++;
    } else {
      if (l != 0) {
        l = phi[l - 1];
      } else {
        phi[r] = 0;
        r++;
      }
    }
  }
}
bool kmp(char text[], char p[]) {
  preFun(p);
  int u = 0, d = 0;
  int m = strlen(text);
  int n = strlen(p);
  while (u < m && d < n) {
    if (text[u] == p[d]) {
      u++;
      d++;
    } else {
      if (d != 0) {
        d = phi[d - 1];
      } else {
        u++;
      }
    }
  }
  if (d == n)
    return true;
  else
    return false;
}
void slove() {
  int n, x, y;
  cin >> n;
  int ma = 0, sum = 0, cnt = 1;
  vector<pair<int, int> > a;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    a.push_back({y, x});
  }
  sort(a.rbegin(), a.rend());
  for (int i = 0; i < n; i++) {
    if (a[i].first == 0 && cnt > 1) {
      sum += a[i].second;
      cnt--;
    } else if (a[i].first == 0 && cnt == 1) {
      ma = max(a[i].second, ma);
    } else if (a[i].first != 0 && cnt != 1) {
      sum += a[i].second;
      cnt += a[i].first;
      cnt--;
    } else {
      sum += a[i].second;
      cnt = a[i].first;
    }
  }
  cout << sum + ma << "\n";
}
int main() { slove(); }
