The Brave fights the dragon
There is an n-headed dragon in your kingdom, and you want to hire some knights to kill it (chop off all the heads).
There are M knights in the village who can hire. A knight with a power value of X can cut off a dragon a salute to the head of not more than X, and the need to pay X gold.
How to hire a knight to chop off all the Dragons ' heads. and the minimum amount of gold required to pay? Note that a knight can only chop a single head (and not be hired two times).
Input format: input includes multiple sets of data.
The first behavior of each set of data is positive integers n and M (1<=n,m<=20000), and the following n lines each behavior an integer. That is, the diameter of each head of an evil dragon. The following M-line each behaves an integer, that is, the ability of each knight.
The input end flag is n=m=0.
Output format: For each set of data. Minimum cost of output. Assuming no solution, output "Loowater is doomed!".
Example input:
2 3
5
4
7
8
4
2 1
5
5
10
0 0
Example output:
11
Loowater is doomed!
Solution: This question is directly as follows: For example, the size of the faucet and the order of the Knight's ability, respectively.
Attached code:
#include <cstdio> #include <iostream> #include <algorithm>using namespace std; #define MAX 20000int Warriors[max];int Dragon[max];int Main () { int i, j, sum; int n, m; while (scanf ("%d%d", &n, &m) = = 2 && n && m) { for (i = 0; i < n; i++) scanf ("%d", &d Ragon[i]); for (j = 0; J < m; j + +) scanf ("%d", &warriors[j]); Sort (dragon, dragon+n); Sort (warriors, warriors+m); The taps and knights from small to large sort j = 0; sum = 0; for (i = 0; i < m; i++) { if (Warriors[i] >= dragon[j]) //Assuming Knight capability is sufficient to be able to cut down this faucet { sum + = Warriors[i]; Employ the Knight J + +; } if (j = = n) break; } if (j = = N) printf ("%d\n", sum); else printf ("Loowater is doomed!\n"); } return 0;}
Execution Result:
C Language daily Training (four)--the Dragon Warrior