题干:

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 — nnand 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

1 2
1 100

Output

0

Input

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

Output

500

Input

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

Output

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.

题目大意:

n个人,m个党派,第i个人开始想把票投给党派pi,而如果想让他改变他的想法需要花费ci元。你现在是党派1,问你最少花多少钱使得你的党派得票数大于其它任意党派。

解题报告:

一般思路想到就是二分+check,不难发现这题是没有单调性的,而且时间复杂度允许,我们就从小到大枚举得票数,维护一个最小话费就行了

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 10000 + 5;
vector<ll > a[MAXN];
ll cnt[MAXN],bk[MAXN];
pair<ll , ll> p[MAXN];
int main()
{int n,m;cin>>m>>n;for(int j = 0; j<n; j++) {scanf("%lld%lld",&p[j].second,&p[j].first);p[j].second--;cnt[p[j].second]++;}sort(p,p+n);ll ans = LLONG_MAX;for(int v = 1; v <=n; v++) {memset(cnt,0,sizeof(cnt));memset(bk,0,sizeof(bk));for(int j = 0; j<n; j++) cnt[p[j].second]++;ll chu = cnt[0],cost = 0;for(int i = 0; i<n; i++) {if(p[i].second !=0 && cnt[p[i].second ] >= v) {cost += p[i].first;chu++;cnt[p[i].second]--;bk[i] = 1;}}for(int i = 0; i<n; i++) {if(p[i].second !=0 && chu < v && bk[i] == 0) {chu++;cost += p[i].first;}}ans = min(ans,cost);}printf("%lld\n",ans);return 0 ;
}

【CodeForces - 1020C】Elections (枚举投票数,贪心)相关推荐

  1. Codeforces Problem-1593A Elections

    Codeforces Problem-1593A Elections 题目链接 基本思想: 题目比较简单,直接暴力枚举即可. AC代码: #include <bits/stdc++.h> ...

  2. CodeForces - 1395D - Boboniu Chats with Du 贪心

    CodeForces - 1395D - Boboniu Chats with Du 贪心 题意:如果ai>ma_i>mai​>m,并且当天可以说话,则接下来ddd天不能说话.其余所 ...

  3. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  4. codeforces#320(div2) D Or Game 贪心

    codeforces#320(div2) D  "Or" Game  贪心 D. "Or" Game time limit per test 2 seconds ...

  5. 投票列表排序 - 默认,投票数

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. 投票|微信投票如何刷投票数的方法和步骤【禁止非法使用】

    教大家微信投票怎么样刷票让票数变多,快速投票怎么收费?最近看到好多人都在网上找怎么提高微信投票数的方法及微信刷票方法,现在的确是越来越多的人在关注这样的怎么提高微信投票数之微信刷票方法,基本上都是想要 ...

  7. 【ZOJ - 3715】Kindergarten Election(枚举得票数,贪心)

    题干: At the beginning of the semester in kindergarten, the n little kids (indexed from 1 to n, for co ...

  8. CodeForces - 1208F Bits And Pieces(SOSdp+贪心)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,求出贡献最大的三元对:(i,j,k)(i,j,k)(i,j,k),满足 i<j<ki<j<ki<j<k ...

  9. Codeforces 846 B Math Show DFS + 贪心

    题目链接: http://codeforces.com/contest/846/problem/B 题目描述: 有N个节点, 每个节点有相同的K个子节点, 每个子节点有时间花费,完成一个子节点获得1分 ...

最新文章

  1. java 连接oracle数据库(最简单的使用)
  2. 云计算之路-阿里云上:RDS用户的烦恼
  3. .NET 框架与多线程 (转载)
  4. zipoutputstream压缩文件响应到浏览器_现代浏览器内部机制之导航这件小事
  5. Spring AOP编程-aspectJ注解开发
  6. CSS3制作一个简单的进度条
  7. Postgres主进程文件—postmaster.pid
  8. CSS中的contenteditable属性
  9. 编程修养 阅读笔记四
  10. 内存颗粒和闪存颗粒的区别_国产闪存颗粒终于熬出头 紫光存储S100固态硬盘评测...
  11. code review平台Rietveld应用指南
  12. 华为hcie题库有多少题?华为认证hcie面试需要注意什么?
  13. 计算机英语 st,1st、2nd、3rd、…10th都是什么的缩写?怎么读?10th之...-1st-英语-司俜辰同学...
  14. 电脑鼠标右键没有新建选项怎么办?
  15. 转【测试基础】测试计划如何编写
  16. ERP: ERP系统的作用
  17. 计算机上无线网络开关在哪里,笔记本无线网络开关在哪里
  18. echarts实现柱状图不同颜色 柱状图横向纵向展示
  19. [CTF] 攻防世界MISC高手区部分题目WriteUp(2)
  20. 汽车软件开发相关词汇

热门文章

  1. 解读设计模式----命令模式(Command Pattern)
  2. 自动化测试框架:没有Surprise的原因
  3. [Leedcode][JAVA][第289题][生命游戏]
  4. Java学习笔记5-2——多线程
  5. mysql5.6.25密码_安装压缩版mysql5.6.25/ 5.7.14
  6. java 当天唯一ids_java-为不同的时间字符串接收相同的ZonedDat...
  7. idea中没有j2ee_idea神器功能大全
  8. wireshark分析SIP协议——注册
  9. ffmpeg编译 MingW + MSYS
  10. 添加蜂窝注册表及永久存储