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

CodeForces - 457C:Elections(三分)的更多相关文章

  1. Codeforces C. Elections(贪心枚举三分)

    题目描述: C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Codeforces 458C - Elections

    458C - Elections 思路: 三分凹形函数极小值域 代码: #include<bits/stdc++.h> using namespace std; #define ll lo ...

  3. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  4. CodeForces - 1059D——二分/三分

    题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...

  5. Codeforces 939E Maximize ( 三分 || 二分 )

    题意 : 给出两个操作,① 往一个序列集合(初始为空)里面不降序地添加数字.② 找出当前序列集合的一个子集使得 (子集的最大元素) - (子集的平均数) 最大并且输出这个最大差值 分析 :  首先关注 ...

  6. Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] C. Weakness and Poorness 三分 dp

    C. Weakness and Poorness Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  7. codeforces 782B The Meeting Place Cannot Be Changed (三分)

    The Meeting Place Cannot Be Changed Problem Description The main road in Bytecity is a straight line ...

  8. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  9. codeforces 578c - weekness and poorness - 三分

    2017-08-27 17:24:07 writer:pprp 题意简述: • Codeforces 578C Weakness and poorness• 给定一个序列A• 一个区间的poornes ...

随机推荐

  1. list_01

    双向链表 不支持随机存取([?] / at(?)) A.头尾 添加/移除 A.1.list::push_back(elemValue); A.2.list::pop_back(); A.3.list: ...

  2. 使用MyBatis Generator自动生成实体、mapper和dao层

    原文链接 通过MyBatis Generator可以自动生成实体.mapper和dao层,记录一下怎么用的. 主要步骤: 关于mybatis从数据库反向生成实体.DAO.mapper: 参考文章:ht ...

  3. python模块——re模块(简单的计算器功能实现_eval版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ = "loki" # Usage: Make a Diy Calcu ...

  4. JavaScript--Array; Array.prototype

    Array: Javascritp中Array的方法: findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. find() 方法返回数组中满足提供的测试函数的第一个 ...

  5. 12月17日周日 form_for的部分理解。belongs_to的部分理解

    1.lean guide:helper method query ,✅

  6. CF-499div2-E-裴蜀定理

    E. Border time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  7. HDU-5001 Walk (概率DP)

    Problem Description I used to think I could be anything, but now I know that I couldn't do anything. ...

  8. ASP.NET的路由系统

    一.URL与物理文件的分离 1.URL与物理文件的分离 对于一个 ASP.NET Web Form应用来说,任何一个请求都对应着某个具体的物理文件.部署在Web服务器上的物理文件可以是静态的(比如图片 ...

  9. ThreadPoolExecutor类

    首先分析内部类:ThreadPoolExecutor$Worker //Worker对线程和任务做了一个封装,同时它又实现了Runnable接口, //所以Worker类的线程跑的是自身的run方法 ...

  10. ES 分布式搜索

    ES整个查询过程是scatter/gather的过程,具体如下: 图见 https://blog.csdn.net/thomas0yang/article/details/78572596?utm_s ...