求条QAQ
查看原帖
求条QAQ
1752560
dabianxjy楼主2025/8/30 13:31

#7 #21 #26WA

#include<bits/stdc++.h>
using namespace std;
struct QAQ {
	int p;
	int c;
} a[1010];
bool cmp(QAQ x, QAQ y) {
	return x.c < y.c;
}
int n, m, ans = 0, id = 0, cnt[1010];
int main() {
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		int p, c;
		cin >> p >> c;
		cnt[p]++;
		if (p != 1) a[id++] = {p, c};
	}
	sort(a, a + id, cmp);
	int mx = 0;
	for (int i = 2; i <= n; i++)if (cnt[i] > mx) mx = cnt[i];
	int nd = max(0, mx - cnt[1] + 1);
	for (int i = 0; i < id && nd > 0; i++) {
		ans += a[i].c;
		cnt[1]++;
		cnt[a[i].p]--;
		nd--;
		mx = 0;
		for (int j = 2; j <= n; j++)if (cnt[j] > mx) mx = cnt[j];
		nd = max(0, mx - cnt[1] + 1);
	}
	cout << (cnt[1] > mx ? ans : 0) << endl;
	return 0;
}
2025/8/30 13:31
加载中...