#include <bits/stdc++.h>
using namespace std;
template <class P, class Q>
void smin(P &a, Q b) {
  if (b < a) a = b;
}
template <class P, class Q>
void smax(P &a, Q b) {
  if (b > a) a = b;
}
template <class P, class Q>
bool in(const P &a, const Q &b) {
  return a.find(b) != a.end();
}
struct vote {
  int candidate;
  int person;
  int cost;
};
struct vote_by_cost_inc {
  bool operator()(vote a, vote b) const {
    if (a.cost != b.cost) {
      return a.cost < b.cost;
    }
    if (a.candidate != b.candidate) {
      return a.candidate < b.candidate;
    }
    return a.person < b.person;
  }
};
set<pair<int, int> > most_voted_candidates;
map<int, set<vote, vote_by_cost_inc> > candidate_votes;
set<vote, vote_by_cost_inc> cheap_votes;
set<vote, vote_by_cost_inc> bribed_votes;
long long my_costs;
int my_inital_votes = 0;
int get_most_voted_votes() {
  if (most_voted_candidates.empty()) {
    return 0;
  }
  return most_voted_candidates.rbegin()->first;
}
int get_most_voted_candidate() {
  return most_voted_candidates.rbegin()->second;
}
void pre_update_candidate_vote(int candidate) {
  most_voted_candidates.erase(
      pair<int, int>(((int)(candidate_votes[candidate]).size()), candidate));
}
void post_update_candidate_vote(int candidate) {
  most_voted_candidates.insert(
      pair<int, int>(((int)(candidate_votes[candidate]).size()), candidate));
}
void add_candidate_vote(vote v) {
  pre_update_candidate_vote(v.candidate);
  candidate_votes[v.candidate].insert(v);
  post_update_candidate_vote(v.candidate);
}
void remove_candidate_vote(vote v) {
  pre_update_candidate_vote(v.candidate);
  candidate_votes[v.candidate].erase(v);
  post_update_candidate_vote(v.candidate);
}
void remove_cheap_vote(vote removable) {
  if (cheap_votes.find(removable) != cheap_votes.end()) {
    my_costs -= removable.cost;
    cheap_votes.erase(removable);
  }
}
void add_cheap_vote(vote addable) {
  my_costs += addable.cost;
  cheap_votes.insert(addable);
}
void bribe_vote(vote bribable) {
  my_costs += bribable.cost;
  bribed_votes.insert(bribable);
}
void consume_most_voted_vote() {
  int candidate = get_most_voted_candidate();
  vote consumable = *candidate_votes[candidate].begin();
  remove_candidate_vote(consumable);
  remove_cheap_vote(consumable);
  bribe_vote(consumable);
}
int get_cheap_votes_count() { return ((int)(cheap_votes).size()); }
void remove_some_cheap_vote() {
  vote removable = *cheap_votes.rbegin();
  remove_cheap_vote(removable);
}
int get_my_current_votes() {
  return my_inital_votes + ((int)(cheap_votes).size()) +
         ((int)(bribed_votes).size());
}
bool has_extra_cheap_votes() { return !cheap_votes.empty(); }
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = (0); i < int(n); ++i) {
    vote vote;
    vote.person = i;
    cin >> vote.candidate >> vote.cost;
    if (vote.candidate == 0) {
      my_inital_votes++;
      continue;
    }
    add_candidate_vote(vote);
    add_cheap_vote(vote);
  }
  long long best_cost = -1;
  for (int threshold = n; threshold >= 0; threshold--) {
    while (get_most_voted_votes() > threshold) {
      consume_most_voted_vote();
    }
    while (has_extra_cheap_votes() && get_my_current_votes() > threshold + 1) {
      remove_some_cheap_vote();
    }
    if (get_my_current_votes() >= threshold + 1) {
      if (best_cost == -1 || my_costs < best_cost) {
        best_cost = my_costs;
      }
    }
  }
  cout << best_cost << endl;
  return 0;
}
