#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct Vote {
  int a, b, rk;
  bool operator<(const Vote& x) const { return b < x.b; }
} vote[MAXN];
int n, m;
vector<int> v[MAXN];
int check(int k) {
  int res = 0, j = 0;
  for (int i = 1; i <= m; i++)
    if (v[i].size() >= k) {
      j += v[i].size() - k + 1;
      res += v[i][v[i].size() - k];
    }
  for (int i = 1; i <= n && j + v[0].size() < k; i++)
    if (vote[i].a &&
        (v[vote[i].a].size() < k || vote[i].rk > v[vote[i].a].size() - k)) {
      j++;
      res += vote[i].b;
    }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &vote[i].a, &vote[i].b);
    m = max(m, vote[i].a);
  }
  sort(vote + 1, vote + n + 1);
  for (int i = 1; i <= n; i++) {
    int tar = vote[i].a;
    vote[i].rk = v[tar].size();
    v[tar].push_back(vote[i].rk ? v[tar][vote[i].rk - 1] + vote[i].b
                                : vote[i].b);
  }
  int l = 1, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid) < check(mid + 1))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d", check(l));
  return 0;
}
