CodeForces(503) 1019A Elections(枚举)

版权声明:选经典题目,写精品文章. https://blog.csdn.net/nka_kun/article/details/81605368

                                                                                Elections

                                                                       time limit 2 seconds

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

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

题意:给出每个人的初始投票对象,和改变每个人投票对象的花费,问让1团队赢的最小花费.

思路:枚举总共改变的人数sum,如果一个团队被支持的人数比1号团队多的数量k大于等于总共改变的人数,那么就需要至少改变此团队的人数为k-sum+1,如果最后还有剩余,再按照花费从小到大来,sum从0枚举到n,取最小值.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct P
{
	int v;
	int pos;
	int par;
	P(){}
	P(int v,int pos,int par):v(v),pos(pos),par(par){}
	friend bool operator < (P x,P y)
	{
		return x.v< y.v;
	}
}b[5321];

struct node
{
	int num;
	vector<P> a;
	friend bool operator < (node x,node y)
	{
		return x.num< y.num;
	}
} e[5321];

int n,m; 
int vis[5321];

ll solve(int sum)
{
	mem(vis,0);
	ll ans = 0;
	int k = e[1].num;
	int tmp = sum;
	for(int i = 2;i<= m;i++)
	{
		if(e[i].num-k>= sum)
		{
			int all = e[i].num-k-sum+1;
			tmp-= all;
			for(int j = 0;j< all&&j< e[i].a.size();j++)
			{
				vis[e[i].a[j].pos] = 1;
				ans+= e[i].a[j].v;
			}
			if(tmp< 0) break;
		}
	}
	
	if(tmp< 0) return (ll)1e15;
	for(int i = 1;(i<= n)&&tmp> 0;i++)
	{
		if(vis[b[i].pos]||b[i].par == 1) continue;
		ans+= b[i].v;
		tmp--;
	}
	return ans;
}

int main()
{
	cin>>n>>m;
	
	for(int i = 1;i<= n;i++)
	{
		int x,y;
		scanf("%d %d",&x,&y);
		e[x].num++;
		e[x].a.push_back(P(y,i,x));
		b[i] = P(y,i,x);
	}
	
	for(int i = 1;i<= m;i++)
		sort(e[i].a.begin(),e[i].a.end());
	
	sort(e+2,e+m+1);
	sort(b+1,b+n+1);
	
	ll ans = (ll)1e15;
	for(int i = 0;i<= n;i++)
		ans = min(ans,solve(i));
	
	cout<<ans<<endl;
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/81605368
今日推荐