Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem.
The shores of Rellau Creek in central Loowater had all been a prime breeding ground for geese. Due to the lack of predators, the geese population is out of control. The people of Loowater mostly kept clear of the geese. Occasionally, a goose would attack one of the people, and perhaps bite off a finger or both, but in general, the people Tol Erated the geese as a minor nuisance.
One day, a freak mutation occurred, and one of the geese spawned a multi-headed fire-breathing dragon. When the dragon grew up, he threatened to burn the kingdom of Loowater to a crisp. Loowater had a major problem. The king was alarmed, and called in his knights to slay the Dragon and save.
The Knights explained: "To slay the dragon, we must chop off all its heads. Each knight can chop off one of the Dragon ' s heads. The heads of the dragon are of different sizes. In order to chop off a head, a knight must is at least as tall as the diameter of the head. The Knights ' union demands that for chopping off a head, a knight must is paid a wage equal to one gold coin for each cent Imetre of the Knight ' s height. "
Would there is enough knights to defeat the dragon? The king called on he advisors to help him decide how many and which knights to hire. After has lost a lot of money building Mir Park, the king wanted to minimize the expense of slaying. As one of the advisors, your job is to help the king. You took it very seriously:if your failed, you and the whole kingdom would is burnt to a crisp!
Input
The input contains several test cases. The first line of all test case contains integers between 1 and 20000 inclusive, indicating the number N of Heads tha T the Dragon has, and the number m of knights in the kingdom. The next n lines each contain a integer, and give the diameters of the Dragon ' s heads, in centimetres. The following m lines each contain a integer, and specify the heights of the Knights of Loowater, also in centimetres.
The last test case was followed by a line containing ' 0 0 '.
Output
For each test case, output a line containing the minimum number of gold coins that the king needs to pay to slay the Drago N. If It is not a possible for the Knights of Loowater to slay the dragon, the output of the line ' Loowater is doomed! '.
Sample Input
2 3
5
4
7
8
4
2 1
5
5
10
0 0
Sample Output
11
Loowater is doomed!
problem-solving idea: According to the data given to find out all the Dragons to kill all the minimum amount of gold coins to be paid. First, the knight wants to cut off the head of the dragon must be the condition that his height is not smaller than the diameter of the dragon's head. A knight can only kill one dragon at most, so when the number of knights is smaller than the number of dragons, the end is only one, that is, the dragon will kill the king. The use of greedy ideas, you can solve the problem, the first to sort the array, it is important to note that each set of data at the beginning of the array to zero, and calculate the number of killed Dragons this change is also to be zeroed.
Program code:
#include<cstdio>#include<algorithm>#include<cstring>UsingNamespace Std;Constint N=20010;int a[N],b[N];IntMain(){int nM, IJ, CountA;While(scanf("%d%d", &n, &m)==2&&n&&m){Memset(A,0,sizeof(A));Memset(b,0,sizeof(b));For(I=0; I<n; I++)scanf("%d", &a[I]);For(I=0; I<m; I++)scanf("%d", &b[I]);Sort(AA+n);Sort(bB+m); CountA=0;int cnt=0;If(n>m)Printf("Loowater is doomed!\ n");Else{For(I=0J=0; I<n&&j<m; I++,j++){If(A[I]>b[j]){i--;Continue;}Else{CountA+=b[j];cnt++;}If(I<n-1&&j==m-1break if (Cnt< N printf (\n "); else printf ( "%d\n" ,counta } return 0 /span>
General problem Solving Techniques [examples]~a