#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int temp;
int n, cnt[N];
pair<int, int> arr[N];
vector<int> vec[N];
set<pair<int, int> > st;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &arr[i].second), scanf("%d", &arr[i].first);
    temp += arr[i].first;
  }
  cnt[0] = n;
  sort(arr, arr + n, greater<pair<int, int> >());
  for (int i = 0; i < N - 1; ++i) st.insert(pair<int, int>(0, i + 1));
  int out = temp;
  int i = 0;
  for (int newCnt = n - 1; i < n && newCnt > 1; newCnt--) {
    while (st.rbegin()->first >= newCnt) {
      int cur = st.rbegin()->second;
      temp += vec[cur].back();
      vec[cur].pop_back();
      ++cnt[0];
      --cnt[cur];
      st.erase(--st.end());
      st.insert(pair<int, int>(cnt[cur], cur));
    }
    while (i < n && cnt[0] > newCnt) {
      if (cnt[arr[i].second] + 1 < newCnt) {
        st.erase(pair<int, int>(cnt[arr[i].second], arr[i].second));
        temp -= arr[i].first;
        vec[arr[i].second].push_back(arr[i].first);
        cnt[0]--;
        cnt[arr[i].second]++;
        st.insert(pair<int, int>(cnt[arr[i].second], arr[i].second));
      }
      i++;
    }
    if (cnt[0] == newCnt) out = min(out, temp);
  }
  cout << out;
}
