欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Codeforces 1020C. Elections 枚举

程序员文章站 2022-06-09 17:41:39
...

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 cicibytecoins 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>
#define ll long long
#define pb push_back 
#define rep(i, j, k) for (int i=j; i<k; i++)
using namespace std;
const ll INF = 1e18;
const int maxn = 3100;

struct Party
{
	int x, cnt;
	bool operator < (const Party& b){
		return cnt<b.cnt; 
	}
}party[maxn];


int x, y;
int n, m, p[maxn], c[maxn];
ll ans, daan;
int cost[maxn][maxn];
ll sum[maxn][maxn];
int used[maxn], lft[maxn];


int main(){
	cin>>n>>m;
	for (int i=1; i<=m; i++){
		party[i].x = i;
		party[i].cnt = 0;
	}
	rep(i, 0, n){
		scanf("%d%d", &x, &y);
		party[x].cnt++;
		cost[x][party[x].cnt-1] = y;
	}
	for (int i=2; i<=m; i++){
		sort(cost[i], cost[i]+party[i].cnt);
		//printf("sort cost[%d]\n", i);
	}
	sort(party+2, party+m+1);
		//puts("hi");
	if (party[1].cnt > party[m].cnt || m==1){
		puts("0");
		return 0;
	}
	/*
	rep(i, 1, m+1){
		printf("this is party %d\n", party[i].x);
		for (int j = 0; j<party[i].cnt; j++){
			printf("cnt = %d cost[%d][%d] = %d\n", party[i].cnt, i, j, cost[party[i].x][j]);
		}
	}
	*/
	for (int i=2; i<=m; i++){
		sum[i][0] = cost[i][0];
		for (int j=1; cost[i][j]; j++){
			sum[i][j] = sum[i][j-1] + cost[i][j];
			//printf("sum[%d][%d] = %lld\n", i, j, sum[i][j]);
		}
	}
	daan = 0x7fffffffffffffff;
	//puts("hi");
	for (int i=max(party[1].cnt-1, 0); i<=n/2; i++){//dier zhenghao de zhemeduo
		int v1 = party[1].cnt, tot = 0;
		ans = 0;
		memset(used, 0, sizeof(used));
		for (int j=m; j>=2; j--){
			if (party[j].cnt >= i){
				v1 += party[j].cnt - i;
				used[j] = party[j].cnt - i;
				ans += sum[party[j].x][used[j]-1];
				//printf("party[%d].x = %d ans = %lld\n", j, party[j].x, ans);
			}
			for (int k=used[j]; k<party[j].cnt; k++){
				lft[tot++] = cost[party[j].x][k]; 
			}
		}
		sort(lft, lft+tot); int pos = 0;
		//printf("tot = %d\n", tot);
		while (v1 <= i && pos<tot){
			ans += lft[pos]; pos++; v1++;
		}
		if (v1>i) daan = min(daan, ans);
		//printf("i = %d ans = %lld\n", i, ans);
	}
	cout<<daan<<endl;
}

md运算符重载会RE?

Codeforces 1020C. Elections 枚举

左re右ac ??????