Codeforces Round #503 (by SIS, Div. 2) - C. Elections - 枚举+贪心

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<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#define ll long long
using namespace std;

struct A{
	int p,c;
}peo[3005];
int piao[3005]={0};//记录每个政党的票数
int num[3005],flag[3005];//记录需要从别的政党支持者中买的票数 
bool cmp(A a,A b){
	return a.c<b.c;
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	int k=0,p,c;
	for(int i=0;i<n;i++){
		scanf("%d%d",&p,&c);
		piao[p]++;
		if(p==1)continue;
		peo[k].p=p;
		peo[k++].c=c;
	} 
	sort(peo,peo+k,cmp);
	ll ans;
	for(int i=piao[1];i<=n;i++){//枚举政党1获胜时的票数 
		int mai1=i-piao[1],mai2=0;
		for(int j=2;j<=m;j++){
			if(piao[j]<i){
				num[j]=0;
				flag[j]=0;
			}
			else{
			    num[j]=piao[j]-i+1;//记录需要从票数比政党1获胜时票数还高的政党买的票数 
			    flag[j]=1;
				mai2+=num[j]; 
		    }
		}
		if(mai1<mai2)continue;//总共需要买的票比需要从高票数那买的票还少,不存在
		int p=mai1-mai2;//需要从票数低于i的政党中买的票数
		//肯定那几个票数比i高的我们都需要买票使它票数等于i-1
		//但我们还需要从几个票数比i小的里买p张票,这时,我们贪心地选择花钱少的买 
		long long tmp=0;
		for(int j=0;j<k;j++){
			if(num[peo[j].p]){
				tmp+=peo[j].c;
				num[peo[j].p]--;
			}
			else if(p){
				tmp+=peo[j].c;
				p--;
			}
		} 
		if(i==piao[1])ans=tmp;
		else ans=min(ans,tmp);
	}
	cout<<ans<<endl;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值