1019A - Elections - CodeForces Solution


brute force greedy *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
 
const ll INF = (ll)1e16;
 
const int N = 3030;
int n, m, k;
pii a[N];
bool used[N];
vector<int> b[N];
int hav;
 
int main(){
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		if (x == 1)
			hav++;
		else
			a[k++] = mp(y, x - 1);
	}
 
	ll ans = INF;
 
	sort(a, a + k);
	for (int i = k - 1; i >= 0; i--)
		b[a[i].second].push_back(i);
 
	for (int w = 0; w <= n; w++) {
		for (int i = 0; i < k; i++)
			used[i] = false;
		int cnt = hav;
		ll cur = 0;
		for (int id = 1; id < m; id++) {
			for (int i = (int)b[id].size() - 1; i >= w; i--) {
				cur += a[b[id][i]].first;
				used[b[id][i]] = 1;
				cnt++;
			}
		}
		for (int i = 0; cnt <= w && i < k; i++) {
			if (used[i]) continue;
			cnt++;
			cur += a[i].first;
		}
		if (cnt > w)
			ans = min(ans, cur);
	}
	printf("%lld\n", ans);
 
	return 0;
}


Comments

Submit
0 Comments
More Questions

1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction
1270B - Interesting Subarray
478C - Table Decorations
1304C - Air Conditioner
1311C - Perform the Combo
1519C - Berland Regional
361A - Levko and Table
5E - Bindian Signalizing