#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long inf = 4e18 + 1;
inline long long read() {
  long long f = 1, x = 0;
  char ch = getchar();
  while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
  while (isdigit(ch)) x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return f * x;
}
struct Node {
  long long p, va, id, qp;
} q[400007];
long long n, m, c[400007], vis[400007], tp;
bool cmp0(Node x, Node y) { return x.va < y.va; }
bool cmp1(Node x, Node y) { return x.p == y.p ? x.va > y.va : x.p < y.p; }
bool cmp2(Node x, Node y) { return x.qp == y.qp ? x.va > y.va : x.qp > y.qp; }
signed main() {
  cin >> n;
  long long sq = 0, tmp = 0;
  for (long long i = 1; i <= n; ++i) {
    long long x, y;
    x = read();
    y = read();
    x++;
    if (x == 1)
      sq++;
    else
      ++tmp, q[tmp].p = x, q[tmp].va = y;
  }
  n = tmp;
  sort(q + 1, q + 1 + n, cmp0);
  for (long long i = 1; i <= n; ++i) q[i].id = i, c[i] = q[i].va;
  sort(q + 1, q + 1 + n, cmp1);
  for (long long i = 1; i <= n; ++i)
    if (q[i].p == q[i - 1].p)
      q[i].qp = q[i - 1].qp + 1;
    else
      q[i].qp = 1;
  sort(q + 1, q + 1 + n, cmp2);
  long long hd = 0, hd2 = 1, sd = 0, sd2 = 0, v1 = 0, v2 = 0;
  for (long long i = 1; i <= n - sq; ++i) hd++, sd++, v1 += c[i], vis[i] = 1;
  long long ans = inf;
  if (q[hd2].qp < max(n, sq)) ans = v1;
  for (long long i = n; i >= 1; --i) {
    while (q[hd2].qp >= i) {
      sd2++;
      if (vis[q[hd2].id] == 1)
        vis[q[hd2].id] = 2, sd--;
      else
        vis[q[hd2].id] = 2, v2 += c[q[hd2].id];
      hd2++;
    }
    while (sd + sd2 + sq > i && sd > 0) {
      if (vis[hd] == 2)
        hd--;
      else
        v1 -= c[hd], vis[hd] = 0, hd--, sd--;
    }
    while (sd + sd2 + sq < i && hd < n) {
      if (vis[hd + 1] == 2)
        hd++;
      else
        v1 += c[hd + 1], vis[hd + 1] = 1, hd++, sd++;
    }
    ans = min(ans, v1 + v2);
  }
  cout << ans << "\n";
}
