Poj 1827 a bunch of monsters greedy (query set optimization)

Source: Internet
Author: User

Description

Background 
Jim is a brave explorer. one day, he set out for his next destination, a mysterious Hill. when he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from continuing his trip by the residents near the hill. nevertheless, our Jim was so brave that he wocould never think of giving up his authentication.
The monsters do exist! When he got into that hill, he was caught by a bunch of fearful monsters.
Fortunately, the monsters didn't plan to kill him or eat him for they were planning a big party. they wanted to invite Jim, a clever human being, to their party, in order to let human beings know that the monsters also have wonderful parties.
Problem 
At the end of the party, the monsters promised that, after the last game, they wocould set Jim free. The game is described as follow:
1. there are a great between boxes of treasure, which are numbered from 1 to X. one box has the only one number; one number can only appear on one box. furthermore, we can assume that X is infinity, because the monsters have got a lot of treasure from the men they caught.
2. There are n monsters in this game. Each picks up a card randomly. After that, he/she (it ?) Opens it, getting a positive integer number d [I], and cannot change it or pick up another card again. the range of d [I] is from 1 to M. if the I-th monster get the number d [I], he can only get the treasure box numbered equal to or less than D [I]. what's more, one box only can be distributed to one monster; one monster can only get one box.
3. of course, there are always ways to distribute the boxes to the monsters when n monsters get their numbers; and not every monster can get a box in your cases. jim has the right to make the arrangement; however, he also knows that the monsters that don't get the boxes will also punish him.
Jim knows the strength of the N monsters. the I-th one has the strength s [I]. we call the sum of strength s [I] Of all the monsters that don't get the boxes --- the damage to Jim. your task is to help Jim find out the minimum damage to him.

Input

The input consists of several test cases. in the first line of each test case, there are two positive integers n and M (1 <= n <= 50000, 1 <= m <= 50000 ), indicating the number of monsters and the range of numbers the monsters possibly get on the cards. then there are n integers d [I] (1 <= d [I] <= m) in the following lines, which are the numbers those monsters got. and in the rest lines of one test case, there are other n positive integers s [I] (1 <= s [I] <= 20000), indicating the strength of each monsters. the test case starting with 2 Zeros is the final test case and has no output.

Output

For each test case, print your answer, the minimum damage, in one line without any redundant spaces.

Sample Input

1 1117 76 4 4 2 3 4 310 70 20 60 30 50 400 0

Sample output

050
Question and analysis:
There are n monsters, each of which has a number d [I] and the damage value is s [I]. The monster needs to allocate a box (assign the number of the box <= d [I]), the box number is 1-inf. A strange box is allocated only once.
The greedy idea is here: 1. The higher the damage, the more satisfied it should be. 2. If there are enough boxes, the box obtained by the I-th monster must be d [I] (so as to be used as much as possible). If the box has been allocated, search for a box with a small number. So sort by the damage of monsters.
The unoptimized code is as follows:
 
The greedy idea remains unchanged. The use of and query set optimization is actually a function used to find the root node. The value of the root node (that is, the number that fun (d [I]) represents the nearest number that can be used by d [I. In this way, the current boxes that can be used can be directly found each time, instead of using the for loop one by one.
The Code is as follows:
 
 
 
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.