代码:
#include <bits/stdc++.h>
using namespace std;
long long n, m, p[1001], c[1001], cnt[1001];
vector<int>cs[1001];
long long ans = 1e18;
long long f(int num) {
long long cur = cnt[1], res = 0;
vector<int>tmp;
for (int i = 2; i <= n; i++) {
int b = max(int(cs[i].size() - num + 1), 0);
for (int j = 0; j < b; j++) {
res += cs[i][j];
}
cur += b;
for (int j = b; j < cs[i].size(); j++) {
tmp.push_back(cs[i][j]);
}
}
sort(tmp.begin(), tmp.end());
for (int i = 0; i < num - cur; i++) {
res += tmp[i];
}
return res;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> p[i] >> c[i];
cnt[p[i]]++;
cs[p[i]].push_back(c[i]);
}
for (int i = 1; i <= n; i++) {
sort(cs[i].begin(), cs[i].end());
}
for (int i = max((int)(cnt[1]), 111); i <= m; i++) {
ans = min(ans, f(i));
}
cout << ans << endl;
}
提交记录:https://www.luogu.com.cn/record/194716683 (疑似你谷新bug) (@niuniudundun,我参考了你的代码,为什么还是没AC?)