C. Maxim and Discounts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
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.

 

每次都选择最少的内个discount,然后每次都剩下的中最贵的内几个,然后免费拿次贵的内两个。这样拿的免费的都是尽可能贵的了~

这题re了QAQ比赛结束才发现数组开的时候少打了个0.  跪。。。

附代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int dis[100000]={0};
 5 int arr[100000]={0};
 6 
 7 int cmp(const void *a,const void *b)
 8 {
 9     return *(int *)a-*(int *)b;
10 }
11 
12 int comp(const void *a,const void *b)
13 {
14     return *(int *)b-*(int *)a;
15 }
16 
17 int main(void)
18 {
19     int i, m, n, sum=0, ans=0, flag=0, j, temp, k;
20 
21     scanf("%d", &m);
22     for( i=0; i<m; i++)
23     {
24         scanf("%d", &dis[i]);
25         sum+=dis[i]+2;
26     }
27     qsort( dis, m, sizeof(int), cmp );
28     scanf("%d", &n);
29     for( i=0; i<n; i++)
30         scanf("%d", &arr[i]);
31     qsort( arr, n, sizeof(int), comp );
32     temp=n;
33     k=0;
34     while( temp>0)
35     {
36         temp-=dis[0]+2;
37         k++;
38     }
39     for( i=0; i<k; i++)
40     {
41         for( j=flag; j<flag+dis[0] && j<n ; j++)
42             ans+=arr[j];
43         flag+=dis[0]+2;
44     }
45     printf("%d\n", ans);
46 
47     return 0;
48 }