The Dragon of Loowater
InterviewTime limit1sMemory limit128 MB
Match the smallest knight to each dragon head so that every head is cut by a tall enough knight, minimizing total height paid; report failure if impossible.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Two pointers, Array
- Solved
- No attempts yet
Problem
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 always been a prime breeding ground for geese. Due to the lack of predators, the goose population grew out of control. The people of Loowater mostly kept clear of the geese. Occasionally a goose would attack a person and perhaps bite off a finger or two, but in general the people tolerated the geese as a minor nuisance.
One day a freak mutation occurred, and one of the geese hatched a multi-headed, fire-breathing dragon. When the dragon grew up, it threatened to burn the Kingdom of Loowater to a crisp. The king was alarmed and called on his knights to slay the dragon and save the kingdom.
The knights explained: to slay the dragon, they must chop off all of its heads. Each knight can chop off exactly one head. The heads are of different sizes. To chop off a head, a knight must be at least as tall as the diameter of that head. For chopping off a head, a knight must be paid one gold coin for each centimetre of his height.
Having already lost a fortune building Mir Park, the king wants to slay the dragon at the minimum possible total cost. As one of the king's advisors, help him decide how many and which knights to hire.
Input
The input contains several test cases.
The first line of each test case contains two integers and (): the number of heads the dragon has and the number of knights in the kingdom. Each of the next lines contains one integer, the diameter of a head in centimetres. Each of the following lines contains one integer, the height of a knight in centimetres.
The last test case is followed by a line containing:
0 0
Output
For each test case, output a single line containing the minimum number of gold coins the king must pay to slay the dragon. If the knights of Loowater cannot slay the dragon, output the line:
Loowater is doomed!