#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)1e9 + 10;
const long long int INFLL = (long long int)1e18 + 10;
const long double EPS = 1e-8;
const long double EPSLD = 1e-18;
const long long int MOD = 1e9 + 7;
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
const int MAX_N = 100010;
int n;
vector<int> v[MAX_N];
priority_queue<int, vector<int>, greater<int> > que;
int sz, co;
vector<int> cv;
int ans = INF;
bool cmp(int a, int b) {
  return ((long long int)(v[a]).size()) > ((long long int)(v[b]).size());
}
int main() {
  scanf("%d", &n);
  for (long long int i = (0); i < (long long int)(n); i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    a--;
    if (a != -1) {
      v[a].emplace_back(b);
      co += b;
      cv.emplace_back(a);
    }
  }
  sort((cv).begin(), (cv).end());
  (cv).erase(unique(((cv)).begin(), ((cv)).end()), (cv).end());
  sort((cv).begin(), (cv).end(), cmp);
  for (auto &w : cv) {
    sort((v[w]).begin(), (v[w]).end());
    reverse((v[w]).begin(), (v[w]).end());
  }
  sz = n;
  for (long long int i = (0); i < (long long int)(MAX_N); i++) {
    if (sz > i) chmin(ans, co);
    for (auto &w : cv) {
      co -= v[w][i];
      que.push(v[w][i]);
      sz--;
    }
    while (!cv.empty() && ((long long int)(v[cv.back()]).size()) == i + 1)
      cv.pop_back();
    while (!que.empty() && sz <= i + 1) {
      co += que.top();
      que.pop();
      sz++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
