Codeforces 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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Max int(3000+10)
#define INF 0xf3f3f3f
int n,m;
vector<int> g[Max];
vector<int> ans;
ll tongji(int h) {
	ans.clear();
	ll sum=0,need=h-g[1].size();
	for(int i=2; i<=m; i++) {
		for(int j=0; j<g[i].size(); j++) {
			if(g[i].size()-j>=h) {
				need--;
				sum+=g[i][j];
			} else {
				ans.push_back(g[i][j]);
			}
		}
	}
	if(need<=0)
		return sum;
	sort(ans.begin(),ans.end());
	int j=0;
	for(int i=1; i<=need; i++)
		sum+=ans[j++];
	return sum;

}
int main() {
	scanf("%d%d",&n,&m);
	int x,y;
	for(int i=0; i<n; i++) {
		scanf("%d%d",&x,&y);
		g[x].push_back(y);
	}
	for(int i=1; i<=m; i++)
		sort(g[i].begin(),g[i].end());

	int le=g[1].size(),ri=n,p1,p2;
	while(le+2<ri) {
		p1=(le+ri)/2;
		p2=(p1+ri)/2;
		if(tongji(p1)>tongji(p2))
			le=p1;
		else
			ri=p2;
	}
	ll sum=ll(1e18);
	for(int i=le; i<=ri; i++)
		sum=min(sum,tongji(i));
	printf("%lld\n",sum);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/PinkAir/article/details/81605908