C - Elections

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 — n and m 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 i-th voter ci 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 n and m (1≤n,m≤3000) — the number of voters and the number of parties respectively.

Each of the following n lines contains two integers pi and ci (1≤pi≤m, 1≤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 1.

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 3, 4 and 5 get one vote and party number 2 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.

如果对答案无法直接求解,无法枚举答案来判断答案,我们可以通过枚举别的间接的求解答案,我们无法直接求得那个最最小的花费,发现从哪个角度不管咋搞也无法贪心,所以我们枚举投的票
我们可以二分需要多少张票,然后问题就会转化成已知要多少张票,然后求花费的最小值。
假设我需要 xx 张票,那么,所有票数大于等于 xx 的人的票都得买走,要不然他们就会阻止我当州长;其次,我肯定要尽量买便宜的,所以需要在读入后对票价排序;最后,如果我买了这些票数大于等于 xx 的人的票后还不够 xx 张,那我就不得不从剩下的票池里买了,而且也一定会去买便宜的。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
//#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int  long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

//int dx[4] = {0, 1, 0, -1};
//int dy[4] = {1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<double, int> PDI;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 2e5 + 10;
const int M = 2e7 + 10;
const int mod = 77797;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = acos(-1);

vector<int> a[N];
int n, m;

int check(int u){
	int ans = 0, tot = a[1].size();
	vector<int> nti;
	for (int i = 2; i <= m; i ++){
		int j = 0, k = a[i].size();
		while(k >= u){
			k --;
			ans += a[i][j ++];
			tot ++;
		}
		while(j < a[i].size())   nti.push_back(a[i][j ++]);
	}
	
	sort(nti.begin(), nti.end());
	for (int i = 0; i < nti.size(); i ++){
		if (tot >= u)   break;
		ans += nti[i], tot ++;
	}
	return ans;
}

signed main(){
	scanf("%lld%lld", &n, &m);
	for (int i = 1; i <= n; i ++){
		int u, v;
		scanf("%lld%lld", &u, &v);
		a[u].push_back(v);
	}
	for (int i = 1; i <= m; i ++)    sort(a[i].begin(), a[i].end());
	
	int l = 1, r = n;
	while(l < r){
		int mid = (l + r) / 2;
		if (check(mid) < check(mid + 1))    r = mid;
		else l = mid + 1;
	}
	
	cout << check(l) << endl;
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值