You are running for a governor in a small city in Russia. You ran some polls and did some research, and for every person in the city you know whom he will vote for, and how much it will cost to bribe that person to vote for you instead of whomever he wants to vote for right now. You are curious, what is the smallest amount of money you need to spend on bribing to win the elections. To win elections you need to have strictly more votes than any other candidate.

Input

First line contains one integer n (1 ≤ n ≤ 105) — number of voters in the city. Each of the next n lines describes one voter and contains two integers ai and bi (0 ≤ ai ≤ 105; 0 ≤ bi ≤ 104) — number of the candidate that voter is going to vote for and amount of money you need to pay him to change his mind. You are the candidate 0 (so if a voter wants to vote for you, ai is equal to zero, in which case bi will also be equal to zero).

Output

Print one integer — smallest amount of money you need to spend to win the elections.

Examples

Input

5
1 2
1 2
1 2
2 1
0 0


Output

3


Input

4
1 2
1 2
2 1
0 0


Output

2


Input

1
100000 0


Output

0
#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;
}
