#include <bits/stdc++.h>
using namespace std;
int main() {
int m, n; cin >> m >> n;
multiset<int> arr;
for (int i = 0; i < m; i++) {
int a; cin >> a;
arr.insert(a);
}
//构建每一个村庄的大顶堆
vector<priority_queue<int, vector<int>, less<int>>> a(n);
int cnt = 0;
for (int i = 0, t; i < n; i++) {
cin >> t;
cnt = max(cnt, t);
while (t--) {
int k; cin >> k;
a[i].push(k);
}
}
if (cnt > m) { //如果任务数量大于人员,那么不可完成
cout << "-1" << endl;
return 0;
}
int ans = 0;
while (cnt--) {
int maxtop = 0; //最大任务难度
int person = 0; //最合适的人选
for (int i = 0; i < n; i++) { //找到最大难度的任务
if (a[i].empty()) continue; //如果有些村庄没有任务,那么跳过
maxtop = max(maxtop, a[i].top());
}
//找到能够干掉最大难度的第一个人,也就是找打大于等于maxtop的第一个人--二分
auto it = lower_bound(arr.begin(), arr.end(), maxtop);
if (it == arr.end()) { //如果找不到-不可完成
cout << "-1" << endl;
return 0;
}
person = *it; //找到了这个人,把他删除,然后更新代价
arr.erase(it);
ans += person;
for (int i = 0; i < n; i++) { //完成了所有村庄中最难的任务,把他们删除
if (a[i].empty()) continue; //同样的没有任务跳过
a[i].pop();
}
}
cout << ans << endl;
return 0;
}
蓝桥杯省赛无忧 编程22 冒险者公会
于 2024-02-09 10:44:15 首次发布