C. Elections
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon.

Elections are coming. You know the number of voters and the number of parties — nn and mm respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give ii-th voter cici bytecoins you can ask him to vote for any other party you choose.

The United Party of Berland has decided to perform a statistical study — you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party.

Input

The first line of input contains two integers nn and mm (1≤n,m≤30001≤n,m≤3000) — the number of voters and the number of parties respectively.

Each of the following nn lines contains two integers pipi and cici (1≤pi≤m1≤pi≤m, 1≤ci≤1091≤ci≤109) — the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision.

The United Party of Berland has the index 11.

Output

Print a single number — the minimum number of bytecoins needed for The United Party of Berland to win the elections.

Examples
input

Copy
1 2
1 100
output

Copy
0
input

Copy
5 5
2 100
3 200
4 300
5 400
5 900
output

Copy
500
input

Copy
5 5
2 100
3 200
4 300
5 800
5 900
output

Copy
600
Note

In the first sample, The United Party wins the elections even without buying extra votes.

In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties 33, 44 and 55 get one vote and party number 22 gets no votes.

In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.

 #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
using namespace std;
#define ll long long
#define gep(i,a,b) for(int i=a;i<=b;i++)
#define gepp(i,a,b) for(int i=a;i>=b;i--)
#define gep1(i,a,b) for(ll i=a;i<=b;i++)
#define gepp1(i,a,b) for(ll i=a;i>=b;i--)
#define N 3009
#define ph push_back
#define inf 10000000000000098
vector<int>ve[N];
int n,m;
ll MIN;
ll solve(ll x){
vector<int>se;
ll ret=,cnt=ve[].size();//初始值
gep(i,,m){
int num=ve[i].size();
gep(j,,num-){
if(num-j>=x){//严格大
ret+=ve[i][j];//必须加到party1上,因为加到别的上面,最后还是加到1上。
cnt++;
}
else{
se.ph(ve[i][j]);
}
}
}
if(cnt<x){
sort(se.begin(),se.end());
for(int i=;i<se.size()&&cnt<x;i++){//在从se里找
ret+=se[i];
cnt++;
}
}
//cnt最后一定是>=x的
return ret;
}
int main()
{
int x;
ll y;
scanf("%d%d",&n,&m);
gep(i,,n){
scanf("%d%lld",&x,&y);
ve[x].ph(y);
}
gep(i,,m)
sort(ve[i].begin(),ve[i].end());
MIN=inf;
gep(i,,n){//枚举查找,不能用二分
MIN=min(MIN,solve(i));
}
printf("%lld\n",MIN);
return ;
}

cf 1020 C的更多相关文章

  1. ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'

    凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...

  2. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  3. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  4. ARC下OC对象和CF对象之间的桥接(bridge)

    在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...

  5. [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现

    1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...

  6. CF memsql Start[c]UP 2.0 A

    CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...

  7. CF memsql Start[c]UP 2.0 B

    CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...

  8. CF #376 (Div. 2) C. dfs

    1.CF #376 (Div. 2)    C. Socks       dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...

  9. CF #375 (Div. 2) D. bfs

    1.CF #375 (Div. 2)  D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...

随机推荐

  1. [转]eclipse启动tomcat无法访问的解决方法

    这篇文章介绍了eclipse启动tomcat无法访问的解决方法,有需要的朋友可以参考一下 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080 ...

  2. 修改datatable列

    var dt = dh.GetPageTable("tabelename"); dt.Columns["TBLNO"].ColumnName = "业 ...

  3. OpenSSL中关于RSA_new和RSA_free的内存泄漏

    这个具体的问题问题代码如下: RSA *rsaKey=RSA_new(); rsaKey = RSA_generate_key(keyBits,,NULL,NULL); RSA_free(rsaKey ...

  4. Android--View事件传递

    Android--View事件传递 View事件传递首先要明白以下要素: 事件就是MotionEvent.该对象包含了传递的事件中的所有信息 事件的来源是Window(即PhoneWindow),包含 ...

  5. Ruby菜鸟入门指南

    写这篇文章的初衷源于我的伙伴们在上手Ruby过程中,表现实在是太让人拙计了.由于项目的急功近利,需要迅速入门Ruby并上手项目.所以很多开发者在实际开发过程中,不熟悉Ruby的表达方式,也会沿用其他语 ...

  6. 微软大礼包 | 集合在线学习资源,助你秒变AI达人

    编者按:人工智能的浪潮正如火如荼地袭来,未来人工智能将大有所为,人们的生活轨迹也正在技术不断向前推进的过程中逐渐改变.人工智能不是科研人员或开发人员的专属,微软希望能够将人工智能带给每个人,从开发者到 ...

  7. 字符串(String)杂谈

    作者:臧圩人(zangweiren) 网址:http://zangweiren.javaeye.com >>>转载请注明出处!<<< 上一次我们已经一起回顾了面试题 ...

  8. Autoit3脚本编写举例

    以任务管理器为例 1.首先打开任务管理器 2.点击结束任务操作 第一步打开任务管理器 run("C:\WINDOWS\system32\taskmgr.exe"); 第二步点击结束 ...

  9. basic ,fundamental ,extreme ,utmost和radical.区别

    basic 普通用词,指明确.具体的基础或起点.fundamental 书面用词,不如basic使用广泛,侧重指作为基础.根本的抽象的事物.radical 着重指事物的根本或其来源.下面是extrem ...

  10. centos上手动安装最新版本ELK

    软件包下载地址:https://www.elastic.co/downloads/elasticsearch 1,安装es #tar zxvf elasticsearch-2.3.4.tar.gz # ...