Codeforces Round #160 (Div. 1) A. Maxim and Discounts

A. 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.

Examples

input
1
2
4
50 50 100 100
output
200
input
2
2 3
5
50 50 50 50 50
output
150
input
1
1
7
1 1 1 1 1 1 1
output
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件物品,就可以获得最多两件不超过购物车里最便宜的两件物品的free。

购物车的数量是无限的,你可以用好多好多。问你最少花多少钱能买到需要的物品。

解法:贪心&思维。首先考虑折扣的选择,显然是qi越小越好。相比于2件物品便宜2件和10件物品便宜2件,必然是选择2件便宜两件。所以首先选出qi中的最小值。然后再将物品排序,因为有价格的要求,最贵的物品肯定不能free,所以从高到低排序,依次将物品加入购物车,到达qi数量要求了就便宜两件物品,直到结束。

这题的难点主要是读题。题意不是很好理解。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <map>
#include <climits>
#define ll long long
using namespace std;
const int mx = 2e6 + 10;
int q;
int a[mx];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m, n;
    cin >> m;
    for(int i = 0; i < m; i++)
    {
        int t; cin >> t;
        if(q == 0) q = t;
        else q = min(q, t);
    }
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> a[i];
    sort(a, a + n, greater<int>());
    int now = 0, sum = 0;
    for(int i = 0; i < n; i++)
    {
        now++;
        sum += a[i];
        if(now == q)
        {
            now = 0;
            i += 2;
        }
    }
    cout << sum << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_37158899/article/details/81178608
今日推荐