54 lines
1.9 KiB
C++
54 lines
1.9 KiB
C++
#include <bits/stdc++.h>
|
||
using namespace std;
|
||
const int N = 1010;
|
||
typedef long long LL;
|
||
int n, m; // n种武器,m种加强材料
|
||
vector<int> c[N]; // c[i][j]表示可以强化第i种武器的材料,修改花费
|
||
LL ans = LONG_LONG_MAX; // 答案,预求最小先设最大
|
||
|
||
/**
|
||
* 计算最终1号武器加强材料的数量为aim时,需要花费的费用
|
||
*/
|
||
LL calc(int aim) {
|
||
int a1 = c[1].size(); // 1号武器已有的加强材料数量
|
||
LL res = 0;
|
||
vector<int> tmp;
|
||
for (int i = 2; i <= n; i++) { // 遍历2号武器到n号武器
|
||
// 当前i号武器需要去掉多少个加强材料,也就是需要付费购买多少个加强材料
|
||
int buy = max((int)c[i].size() - aim + 1, 0);
|
||
for (int j = 0; j < buy; j++)
|
||
res += (LL)c[i][j]; // 这些钱不得不花
|
||
|
||
a1 += buy; // 通过购买,1号武器的加强材料数量上升了!
|
||
// 对于第i种武器,没有购买的加强材料汇总到tmp数组中,因为可能存在
|
||
for (int j = buy; j < c[i].size(); j++)
|
||
tmp.push_back(c[i][j]);
|
||
}
|
||
// 所有没动过的材料进行由小到大排序
|
||
sort(tmp.begin(), tmp.end());
|
||
// 如果数量不足,还需要在这些没动过的材料中取一些
|
||
for (int i = 0; i < aim - a1; i++)
|
||
res += (LL)tmp[i];
|
||
return res;
|
||
}
|
||
int main() {
|
||
#ifndef ONLINE_JUDGE
|
||
freopen("20241202.in", "r", stdin);
|
||
#endif
|
||
|
||
cin >> n >> m;
|
||
for (int i = 1; i <= m; i++) {
|
||
int p, cc;
|
||
cin >> p >> cc;
|
||
c[p].push_back(cc);
|
||
}
|
||
for (int i = 1; i <= n; i++)
|
||
sort(c[i].begin(), c[i].end()); // 每种武器的加强材料按价格由小到大排序
|
||
|
||
// 枚举最终第1种武器的加强材料数量
|
||
for (int i = max((int)c[1].size(), 1); i <= m; i++)
|
||
ans = min(ans, calc(i)); // 计算此时需要花费的金额,最小值就是答案
|
||
|
||
cout << ans << endl;
|
||
return 0;
|
||
} |