ProblemStatement 3
ProblemStatement 3
Dhanananda was an evil king who ruled the kingdom of Magadh. He looted gold and silver
from several places and his own people, by using the following unfair strategy.
There are N shops in Magadh, and the ith shop has Si silver coins. Each silver costs
exactly 1 Panas, which is the unit of currency in the kingdom of Magadh. Dhananand
would choose an integer P, and loots all the shops in several steps. In each step:
He would choose a shop i which hasn't been looted yet, pay the owner exactly Si Panas,
and take away all the silver coins in that shop (Hence, he also ends up looting this shop).
He would now choose atmost P shops which haven't been looted yet and take away all the
silver coins from these shops without paying a single Pana (Yes, he takes all of them for
free).
He repeats the above steps until all the N shops have been looted. Your task is to devise a
strategy for Dhanananda to loot the shops in some order, so that the number of Panas he
has to pay is minimum. You'll also be given multiple values of P (M of them to be precise),
and you need to find the minimum number of Panas for each of these values.
Input
- The first line of input consists of a single integer N denoting the number of shops in
Magadh.
- The second line of input consists of N space separated integers denoting S1, S2, ...,
Sn, where Si denotes the number of silver coins in the ith shop.
- The third line of input consists of a single integer M denoting the number of values of
P to follow.
- Each of the following M lines consist of a single integer, where the value on the ith
line denotes the value of P for the ith query.
Output
- Output exactly M integers on separate lines, where the output on the i-th line denotes
the answer for the i-th value of P.
Constraints
● 1 ≤ N ≤ 105
● 1 ≤ M ≤ 105
● 0 ≤ P ≤ N-1
● 1 ≤ Si ≤ 104
- Time limit per test = 1 sec
- Source Limit = 50 MBytes
- I/O = Standard I/O
SAMPLE INPUTS:
4
3214
2
0
2
SAMPLE OUTPUTS:
10
3
EXPLANATION:
- For the first query,P= 0. Hence, Dhananand cannot take silver coins from any of the
shops for free. It will cost him 3 + 2 + 1 + 4 = 10 Panas.
- For the second query, P = 2. In the first step Dhananand can pay 2 Panas for silver
coins in shop number 2, and take the silver coins in shops 1 and 4 for free (Note that
shop 1 has 3 silver coins and shop 4 has 4 silver coins). Now, he has looted shops 1,
2 & 4. Now in the second step, he loots shop 3, by paying 1 Pana. Hence, the total
cost = 1 + 2 = 3. Note that there might be multiple ways to achieve the minimum cost,
and we have explained only one of them.