You are given N
numbers – , and an integer A
. You can do an unlimited number of swaps in the array by swapping and for a cost of . The new array after the swap would then be . For a certain number R
, your task it to find the minimum sum of the costs of your swaps and . Given Q
queries and an for each of them, find the minimum cost you can achieve for each query. All queries are independent.
Input
There are 3
integers on the first line: N
– the size of the array, Q
– the number of queries and A
– the coefficient is multiplied by. The second line of the input contains N
positive integers - the starting array. The last line holds the positive integers .
Output
On each of the Q
lines output the minimum cost for the corresponding query.
Constraints
Subtask 1 (5 points)
N ≤ 8
Q = 1
Subtask 2 (10 points)
N ≤ 5 000
Q = 1
Subtask 3 (25 points)
Q = 1
Subtask 4 (10 points)
Subtask 5 (50 points)
Examples
stdin
4 1 5
4 1 2 3
2
stdout
25
stdin
4 1 6
4 1 2 3
2
stdout
29
Explanation
Example 1: It is optimal to not swap any elements. The cost is then (4 + 1) × 5 = 25
.
Example 2: We can first swap 4
with 1
and then 4
with 2
:
4 1 2 3
1 4 2 3
1 2 4 3
The costs of the swaps are 4+1=5
and 4+2=6
. Therefore, the total cost is 5+6+(1+2)×6=29
.