cswork / OAOpen practice workspace

IBMOA #61

Minimum Operations to Make All Array Elements Equal

Estimated MediumJudge-ready

Topics (AI-labelled)Prefix & RangesBinary Search

Each operation adds 1 to or subtracts 1 from one element. For each query value, find the minimum number of operations to turn all elements into that value. Queries are independent; the original array is restored before the next query.

Input

The first line contains n q, the second line contains the array, and the third line contains q queries. Full source ranges: 1≤n,q≤100000, and the array values and queries are integers in 1..10^9.

Output

Output q lines of integers in query order.

Example 1

Input:

4 2
3 1 6 8
1 5

Output:

14
10

Example 2

Input:

4 1
2 9 6 3
10

Output:

20

Example 3

Input:

3 3
1 1 1
1 2 1

Output:

0
3
0

Example notes

The examples give 14 and 10; 20; and 0, 3, 0 respectively.

Editorial

Loading solution…