#include <bits/stdc++.h>   using namespace std;   const int L = 0, R = 10000;   int n, vote, tot;   vector<int> p[110000];   pair<int, int> x[110000];   long long sum[110000];   long long he[4 * 110000];   int num[4 * 110000];   long long ask(int root, int l, int r, int k) {     if (k <= 0)       return 0;     else if (num[root] == k)       return he[root];     else if (l == r)       return (long long)k * l;     else {       int mid = (l + r) >> 1;       if (num[2 * root] < k) {         return ask(2 * root, l, mid, num[2 * root]) +                ask(2 * root + 1, mid + 1, r, k - num[2 * root]);       } else {         return ask(2 * root, l, mid, k);       }     }   }   void add(int root, int l, int r, int k) {     if (l > r || k < l || k > r)       return;     else if (l == r) {       num[root]++;       he[root] += k;     } else {       num[root]++;       he[root] += k;       int mid = (l + r) >> 1;       add(2 * root, l, mid, k);       add(2 * root + 1, mid + 1, r, k);     }   }   int main() {     cin >> n;     int mx = 0;     for (int i = 0, a, b; i < n; i++) {       scanf( %d%d , &a, &b);       if (a != 0)         p[a].push_back(b);       else         vote++;     }     for (int i = 1; i < 110000; i++) {       sort(p[i].begin(), p[i].end());       reverse(p[i].begin(), p[i].end());       mx = max(mx, (int)p[i].size());     }     for (int i = 1; i < 110000; i++) {       int m = (int)p[i].size();       for (int j = 0; j < m; j++) {         x[tot++] = pair<int, int>(j + 1, p[i][j]);       }     }     sort(x, x + tot);     for (int i = 0; i < tot; i++) {       if (i == 0)         sum[i] = x[i].second;       else         sum[i] = sum[i - 1] + x[i].second;     }     long long ans = 1e10;     int pre = 0;     for (int i = 0; i <= mx + 1; i++) {       int indx = upper_bound(x, x + tot, pair<int, int>(i, 1e9)) - x;       long long cans = sum[tot - 1] - sum[indx] + x[indx].second;       if (indx == tot) cans = 0;       while (pre < indx) {         add(1, L, R, x[pre].second);         pre++;       }       indx = tot - indx + vote;       if (i >= indx) {         indx = i - indx + 1;         cans += ask(1, L, R, indx);       }       ans = min(ans, cans);     }     cout << ans << endl;     return 0;   }   