Codeforces 261A - Maxim and Discounts
Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are m types of discounts. We assume that the discounts are indexed from 1 to m. To use the discount number i, the customer takes a special basket, where he puts exactly qi items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the “free items” (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected “free items” is as follows: each of them mustn’t be more expensive than the cheapest item out of the qi items in the cart.
Maxim now needs to buy n items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts.
Input:
The first line contains integer m (1 ≤ m ≤ 105) — the number of discount types. The second line contains m integers: q1, q2, …, qm (1 ≤ qi ≤ 105).
The third line contains integer n (1 ≤ n ≤ 105) — the number of items Maxim needs. The fourth line contains n integers: a1, a2, …, an (1 ≤ ai ≤ 104) — the items’ prices.
The numbers in the lines are separated by single spaces.
Output:
In a single line print a single integer — the answer to the problem.
範例:
input:
1 | 1 |
output:
1 | 200 |
input:
1 | 2 |
output:
1 | 150 |
input:
1 | 1 |
output:
1 | 3 |
Note:
In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150.
題意:
現在有m種折扣,可以在買了qi個東西後免費拿最多2個各自單價不超過這qi個東西中最低單價東西的東西,折扣可以重複使用。想買的東西有n個,各自的單價是ai,問你買這n個東西最少的花費是多少?
思路:
因為折扣可以重複使用,所以只要挑一個需要買最少的就好了,然後將n個東西從貴排到便宜,依序購買即可。