用两个优先队列维护龙头的直径和骑士的高度,不断从队列中抛出元素,第一个满足条件的即为最小的。
#include <cstdio>
#include <queue>
using namespace std;
int main(int argc, char const *argv[]) {
int n, m;
while (scanf("%d%d", &n, &m) == 2 && n + m) {
priority_queue<int, vector<int>, greater<int> > head, knight;
for (int i = 0; i < n; i++) {
int dia;
scanf("%d", &dia);
head.push(dia);
}
for (int i = 0; i < m; i++) {
int hei;
scanf("%d", &hei);
knight.push(hei);
}
int sum = 0;
bool ok = true;
while (!head.empty()) {
int dia = head.top();
while (!knight.empty() && knight.top() < dia) {
knight.pop();
}
if (knight.empty()) {
ok = false;
break;
}
sum += knight.top();
knight.pop();
head.pop();
}
if (ok) {
printf("%d\n", sum);
} else {
puts("Loowater is doomed!");
}
}
return 0;
}