CodeForces - 457C:Elections(三分)

时间:2023-03-08 16:32:04

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

题意:给定N个voter,给个人有自己准备投的人ai,以及费用bi,表示可以用bi元钱去收买他他让改变主意。问0号选手至少花多少钱,使得他的voter最多。

思路:不难想到二分0号选手的voter数,但是二分是WA的,因为单调性有个临界值,小于这个临界值,无法保证0号是最多的。 直接三分。

(注意不要用二分,因为并非是收买的人越多,代价越高。 有可能收买的人越多,可供选择的越多,使得低价的越多,所以必须三分。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int num[maxn],ans=<<,N;
vector<int>G[maxn]; int q[maxn],cnt;
int check(int Mid)
{
int tnum=num[]+Mid,res=,tot=;
rep(i,,){
rep(j,,max(-,num[i]-tnum)) tot++,res+=G[i][j];
}
if(tot>Mid) return <<; cnt=; //不足以成为最多,反正一个极大值
rep(i,,){
rep(j,max(,num[i]-tnum+),num[i]-) q[++cnt]=G[i][j];
}
sort(q+,q+cnt+);
rep(i,,Mid-tot) res+=q[i];
return res;
}
int main()
{
int x,y;
scanf("%d",&N);
rep(i,,N){
scanf("%d%d",&x,&y);
if(x==||y==) num[]++;
else G[x].push_back(y),num[x]++;
}
rep(i,,) sort(G[i].begin(),G[i].end());
int F=true;
rep(i,,N) if(num[i]>=num[]) F=false;
if(F) return puts(""),;
int L=,R=N,Mid1,Mid2,f1,f2;
while(L<=R){
Mid1=L+(R-L)/; f1=check(Mid1);
Mid2=R-(R-L)/; f2=check(Mid2);
if(f1<=f2) R=Mid2-,ans=min(ans,f1);
else L=Mid1+,ans=min(ans,f2);
}
printf("%d\n",ans);
return ;
}