Paired Up


Problem Statement

You are given a list of nn integers X=[x1, x2, ..., xn]X = [x_1,\ x_2,\ ...,\ x_n]. Now, imagine you have a list PP of all pairings of integers from 11 to nn such that PP is sorted in increasing order. In other words, you have:

\begin{array}{rcc} P &=& \Big[\ \normalsize(1, 1), (1, 2), (1, 3), ..., (1, n),\ & & \ \ \ (2, 1), (2, 2), (2, 3), ..., (2, n), \ & & \ ...\ & & \ \ \ \ \ \ (n, 1), (n, 2), (n, 3), ..., (n, n)\ \Big]\normalsize \ \end{array}

Now, you are given QQ queries, each will contain a single integer qiq_i such that 1qiP1 \leq q_i \leq |P|, given some qiq_i you should determine f(qi)f(q_i):

\begin{array}{rcl} f(k) &=& \sum_{i=1}^{k} x_{P_i[0]} - x_{P_i[1]} \end{array}

In other words, f(k)f(k) takes the first kk pairs in PP, and for each pair (a,b)(a, b) it will calculate xaxbx_a - x_b and sum all of them together.

Input Format

Your first line will contain two space-separated integers nn and qq

Your next line will contain nn space-separated integers x1x_1 through xnx_n.

Your next QQ lines will contain one integer each, the ithi^{th} of which representing qiq_i.

Output Format

You should output a single integer for each query (in the same order the queries appear), the ithi^{th} of which should be f(qi)f(q_i).

Constraints

  • 1n,q1051 \leq n, q \leq 10^5
  • 1qin21 \leq q_i \leq n^2
  • 103xi103-10^3 \leq x_i \leq 10^3

Sample Cases

Input 1
3 3
11 7 5
4
6
9
Output 1
6
8
0
Explanation 1

Here P=[(1,1),(1,2),(1,3),(2,1),(2,2),(2,3),(3,1),(3,2),(3,3)]P = [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)].

For the first query q1=4q_1 = 4, we calculate (x1x1)+(x1x2)+(x1x3)+(x2x1)=(1111)+(117)+(115)+(711)=6(x_1 - x_1) + (x_1 - x_2) + (x_1 - x_3) + (x_2 - x_1) = (11 - 11) + (11 - 7) + (11 - 5) + (7 - 11) = 6. For the second query q1=6q_1 = 6, we calculate (x1x1)+(x1x2)+(x1x3)+(x2x1)+(x2x2)+(x2x3)(x_1 - x_1) + (x_1 - x_2) + (x_1 - x_3) + (x_2 - x_1) + (x_2 - x_2) + (x_2 - x_3). For the third query q1=9q_1 = 9, we calculate (x1x1)+(x1x2)+(x1x3)+(x2x1)+(x2x2)+(x2x3)+(x3x1)+(x3x2)+(x3x3)(x_1 - x_1) + (x_1 - x_2) + (x_1 - x_3) + (x_2 - x_1) + (x_2 - x_2) + (x_2 - x_3) + (x_3 - x_1) + (x_3 - x_2) + (x_3 - x_3).

Input 2
10 5
-5 10 -1 9 -4 6 3 -1 -7 -5
20
36
8
79
38
Output 2
40
64
-57
126
80

Template

Code 1
n, q = map(int, input().split())
numbers = list(map(int, input().split()))
queries = [int(input()) for _ in range(q)]

# print your output

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.