UVA11292 The Dragon of Loowater

#include
#include
#include
#define M(a) memset(a, 0, sizeof(a))
using namespace std; 
const int maxn = 20000 + 10;
int head[maxn], knight[maxn];
int main() {
	int n, m;
	while(scanf("%d%d", &n, &m) == 2 && n && m) {
		long long ans = 0;
		M(head); M(knight);
		int phead = 0, pknight = 0;
		for(int i = 0; i < n; i++) scanf("%d", &head[i]);
		for(int i = 0; i < m; i++) scanf("%d", &knight[i]);
		sort(head, head + n); sort(knight, knight + m);
		while(phead < n && pknight < m) {
			if(head[phead] <= knight[pknight]) {
				ans += knight[pknight++];
				phead++;
			}
			else pknight++;
		}
		if(phead < n) printf("Loowater is doomed!\n");
		else printf("%lld\n", ans);
	}
	return 0;
}

你可能感兴趣的:(UVA,贪心)