#include <bits/stdc++.h>
using namespace std;
const int Nmax = 1e5 + 5;
vector<int> id;
vector<int> s[Nmax];
pair<int, int> a[Nmax];
int c[Nmax];
int num[Nmax];
int sum[Nmax];
int n, ans, res, cnt, mx, N;
bool cmp(int i, int j) { return s[i].size() > s[j].size(); }
void compress() {
  sort(c + 1, c + n + 1);
  c[0] = -1;
  for (int i = 1; i <= n; i++) {
    if (c[i] != c[N]) c[++N] = c[i];
  }
  for (int i = 1; i <= N; i++) {
    id.push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    int l = 1;
    int r = N + 1;
    while (r - l > 1) {
      int mid = (l + r) >> 1;
      if (c[mid] <= a[i].first)
        l = mid;
      else
        r = mid;
    }
    a[i].first = l;
    swap(a[i].first, a[i].second);
  }
  sort(a + 1, a + n + 1);
}
void update(int i, int x, bool add) {
  while (i <= n) {
    sum[i] += x;
    num[i] += (add) ? 1 : -1;
    i += i & -i;
  }
}
int get(int x) {
  int cur = 0, res = 0, cnt = 0;
  for (int i = 20; i >= 0; i--) {
    if (cur + (1 << i) > n) continue;
    if (cnt + num[cur + (1 << i)] <= x) {
      cur += 1 << i;
      cnt += num[cur];
      res += sum[cur];
    }
  }
  return res;
}
void solve(int x) {
  for (int i : id) {
    if (s[i].size() == x) {
      int j = s[i].back();
      s[i].pop_back();
      update(j, -a[j].first, 0);
      res += a[j].first;
      cnt++;
    } else
      break;
  }
  if (cnt > x)
    ans = min(ans, res);
  else {
    ans = min(ans, res + get(x - cnt));
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    c[i] = a[i].first;
  }
  compress();
  for (int i = n; i >= 1; i--) {
    update(i, a[i].first, 1);
    s[a[i].second].push_back(i);
    mx = max(mx, (int)s[a[i].second].size());
  }
  sort(id.begin(), id.end(), cmp);
  ans = 1e9;
  for (int i = mx + 1; i >= 1; i--) {
    if (cnt > i) break;
    solve(i);
  }
  cout << ans;
  return 0;
}
