Buying cards
InterviewTime limit1sMemory limit256 MB
Given pack prices P_1 to P_N, choose packs whose card counts sum to exactly N to maximize the total price.
- Level
Easy3 of 10
- Topics
- Dynamic programming
- Solved
- No attempts yet
Problem
Collecting PS cards is a fad in Mingyu's neighborhood right now.
A PS card shows the handle and the face of someone well known in problem solving. Each card is painted a color that marks its grade, and there are 8 colors.
- legend card
- red card
- orange card
- purple card
- blue card
- teal card
- green card
- gray card
Cards are sold only in packs. There are kinds of pack: a pack holding 1 card, a pack holding 2 cards, and so on up to a pack holding cards. A pack holding cards costs won. The same kind of pack can be bought any number of times.
Mingyu believes a superstition: a pack with few cards still holds many high grade cards if it is expensive. So he wants to buy cards while paying as much money as possible.
For example, suppose there are 4 kinds of pack and , , , . The largest amount Mingyu can pay for 4 cards is 10 won. He buys the pack of 2 cards twice.
If , , , , buying the pack of 1 card four times costs 20 won, and that is the largest amount.
If , , , , buying the pack of 3 cards and the pack of 1 card costs 18 won, and that is the largest amount.
Given the pack prices, write a program that finds the largest amount Mingyu pays to buy cards. He cannot buy more than cards and throw the extras away to end up with . The card counts of the packs he buys must add up to exactly .
Input
The first line contains the number of cards that Mingyu wants to buy. ()
The second line contains through in order. ()
Output
Print the largest amount Mingyu pays to end up with cards.