luogu#P16422. [IATI 2022] Reorder

[IATI 2022] Reorder

Problem Description

You are given NN numbers v1,v2,,vN- v_1, v_2, \cdots, v_N, and an integer AA. You can do an unlimited number of swaps in the array by swapping viv_i and vi+1v_{i+1} for a cost of vi+vi+1v_i + v_{i+1}. The new array after the swap would then be v1,,vi+1,vi,vNv_1, \cdots , v_{i+1}, v_i \cdots, v_N. For a certain number RR, your task it to find the minimum sum of the costs of your swaps and (v1+v2++vR)×A(v_1 + v_2 + \cdots + v_R) \times A. Given QQ queries and an RiR_i for each of them, find the minimum cost you can achieve for each query. All queries are independent.

Input Format

There are 3 integers on the first line: NN – the size of the array, QQ – the number of queries and AA – the coefficient (v1+v2++vR)(v_1 + v_2 + \cdots + v_R) is multiplied by. The second line of the input contains NN positive integers v1,,vNv_1, \cdots, v_N - the starting array. The last line holds the positive integers R1,,RQR_1, \cdots, R_Q.

Output Format

On each of the QQ lines output the minimum cost for the corresponding query.

4 1 5
4 1 2 3
2
25
4 1 6
4 1 2 3
2
29

Hint

Explanation of the examples

Example 1: It is optimal to not swap any elements. The cost is then (4+1)×5=25(4 + 1) \times 5 = 25.

Example 2: We can first swap 44 with 11 and then 44 with 22:

4 1 2 3

1 4 2 3

1 2 4 3

The costs of the swaps are 4+1=54 + 1 = 5 and 4+2=64 + 2 = 6. Therefore, the total cost is

5+6+(1+2)×6=29.5 + 6 + (1 + 2) \times 6 = 29.

Constraints

  • 1Q,RiN1 \le Q, R_i \le N
  • 1vi,A1061 \le v_i, A \le 10^6

Subtasks

No. Additional constraints < Points
NN Other
1 8\le 8 Q=1Q = 1 5
2 5000\le 5000 10
3 106\le 10^6 25
4 1.5×104\le 1.5 \times 10^4 10
5 5×104\le 5 \times 10^4 50