C Language daily Training (four)--the Dragon Warrior

Source: Internet
Author: User
Tags chop

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.