#include <bits/stdc++.h>
using namespace std;
const int mod1 = 1e5 + 7;
const int N = 1e6 + 7;
const int mod2 = 1e9 + 7;
const long long mod3 = 1e12 + 7;
const int si = 100000;
int n, votes[mod1], cost[mod1];
vector<int> canddt[mod1];
int hm(int mid) {
  int tnum = votes[0] + mid, res = 0, tot = 0;
  for (int i = 1; i <= si; i++) {
    for (int j = 0; j <= max(-1, votes[i] - tnum); j++)
      tot++, res += canddt[i][j];
  }
  if (tot > mid) return 1 << 30;
  int cnt = 0;
  for (int i = 1; i <= si; i++) {
    for (int j = max(0, votes[i] - tnum + 1); j <= votes[i] - 1; j++)
      cost[cnt++] = canddt[i][j];
  }
  sort(cost, cost + cnt);
  for (int i = 0; i < mid - tot; i++) res += cost[i];
  return res;
}
int main() {
  int who, pay, r, l, mid1, mid2, res1, res2, ans = 1 << 30;
  bool flag = 1;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &who, &pay);
    if (pay == 0 || who == 0)
      votes[0]++;
    else {
      votes[who]++;
      canddt[who].push_back(pay);
    }
  }
  for (int i = 1; i <= n; i++)
    if (votes[i] >= votes[0]) {
      flag = 0;
      break;
    }
  if (flag) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= si; i++) sort(canddt[i].begin(), canddt[i].end());
  l = 1;
  r = n;
  while (l <= r) {
    mid1 = l + (r - l) / 3;
    res1 = hm(mid1);
    mid2 = r - (r - l) / 3;
    res2 = hm(mid2);
    if (res1 <= res2)
      r = mid2 - 1, ans = min(ans, res1);
    else
      l = mid1 + 1, ans = min(ans, res2);
  }
  printf("%d\n", ans);
  return 0;
}
