#include <bits/stdc++.h>
using namespace std;
pair<long long, int> ara[100010];
vector<long long> val[100010];
int cnt[100010], can[100010];
long long prin = 0, show, moka, mal;
multiset<int> mat[100010];
set<pair<long long, int> > sota;
int main() {
  ios::sync_with_stdio(false);
  int n, m, a, b;
  cin >> n;
  for (int i = 0; i <= n - 1; i++) {
    cin >> a >> b;
    ara[i] = make_pair(b, a);
  }
  sort(ara, ara + n);
  for (int i = 0; i <= n - 1; i++) {
    if (ara[i].second == 0) continue;
    prin += ara[i].first;
  }
  show = prin;
  moka = n;
  for (int i = n - 1; i >= 0; i--) {
    if (ara[i].first == 0) continue;
    prin -= ara[i].first;
    moka--;
    sota.erase(make_pair(mat[ara[i].second].size(), ara[i].second));
    mat[ara[i].second].insert(ara[i].first);
    sota.insert(make_pair(mat[ara[i].second].size(), ara[i].second));
    while (!sota.empty()) {
      pair<long long, int> use = *(sota.rbegin());
      if (use.first < moka) break;
      sota.erase(use);
      int v = *(mat[use.second].begin());
      prin += v;
      moka++;
      mat[use.second].erase(mat[use.second].find(v));
      use.first--;
      if (use.first) sota.insert(use);
    }
    show = min(show, prin);
  }
  cout << show << endl;
  return 0;
}
