atcoder#ABC288E. [ABC288E] Wish List
[ABC288E] Wish List
Score : points
Problem Statement
There are items in a shop, numbered as Item , Item , , Item . For each , the regular price of Item is yen. For each item, there is only one in stock.
Takahashi wants items: Item , Item , , Item .
He repeats the following until he gets all items he wants.
Let be the number of unsold items now. Choose an integer such that , and buy the item with the -th smallest item number among the unsold items, for its regular price plus yen.
Print the smallest total amount of money needed to get all items Takahashi wants.
Takahashi may also buy items other than the ones he wants.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
5 2
3 1 4 1 5
9 2 6 5 3
3 5
17
Here is a way for Takahashi to get all items he wants with the smallest total amount of money.
- Initially, five items, Items , are remaining. Choose to buy the item with the fifth smallest item number among the remaining, Item , for yen.
- Then, four items, Items , are remaining. Choose to buy the item with the second smallest item number among the remaining, Item , for yen.
- Then, three items, Items , are remaining. Choose to buy the item with the second smallest item number among the remaining, Item , for yen.
Now, Takahashi has all items he wants, Items and , (along with Item , which is not wanted) for a total cost of yen, the minimum possible.
20 8
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12
1 3 4 5 8 14 16 20
533