#P9342. [JOIST 2023] 比太郎之旅 / Bitaro's Travel

[JOIST 2023] 比太郎之旅 / Bitaro's Travel

Description

There is a very long road in JOI City, which can be considered as the real number line. A position on the road is represented by a real number coordinate. In JOI City, there are NN sightseeing spots along the road, numbered from 11 to NN in ascending order of the coordinates. The coordinate of the ii-th sightseeing spot (1iN)(1\le i\le N) is XiX_i.

Bitaro will visit all the sightseeing spots in JOI City. Since "greedy" is the slogan of his life, he will repeat the following procedures until he visits all the sightseeing spots.

  • Let xx be Bitaro’s current coordinate. Among the sightseeing spots he has not yet visited, take the sightseeing spot ii where the distance xXi|x − X_i| from Bitaro's current position takes a minimum value. Then Bitaro moves to the position of the sightseeing spot ii, and visits it. If there are more than one such sightseeing spots, he moves to the sightseeing spot whose coordinate is smaller than the others. Here, t|t| is the absolute value of tt.

However, thanks to long years of experience, Bitaro knows that if he moves by repeating the above procedures, the total traveling distance may be longer than he expected. Since the total traveling distance varies according to the starting coordinate, he wants to know the total traveling distance until he visits all the sightseeing spots if he starts from each of QQ candidates of starting coordinates S1,S2,,SQS_1, S_2,\dots, S_Q.

To help Bitaro, write a program which calculates the total traveling distance if he starts from each of the candidates of starting coordinates, given information of JOI City and candidates of starting coordinates.

Input Format

Read the following data from the standard input.

NN

X1 X2XNX_1\ X_2\cdots X_N

QQ

S1S_1

S2S_2

\vdots

SQS_Q

Output Format

Write QQ lines to the standard output. The jj-th line (1jQ)(1 \le j \le Q) of output should contain the total traveling distance if Bitaro starts from the coordinate SjS_j.

5
0 5 6 7 9
1
7

15
10
1 2 3 4 5 6 7 8 9 10
10
1
2
3
4
5
6
7
8
9
10

9
10
11
12
13
14
15
16
17
9

Hint

【样例解释 #1】

If Bitaro starts from the coordinate 77, he visits all the sightseeing spots as follows.

  1. He has not yet visited the sightseeing spots 1,2,3,4,51, 2, 3, 4, 5, and the distances from Bitaro’s current position are 7,2,1,0,27, 2, 1, 0, 2, respectively. Since the sightseeing spot 44 is the nearest sightseeing spot from Bitaro, he stays at the coordinate 77 and visits the sightseeing spot 44.
  2. He has not yet visited the sightseeing spots 1,2,3,51, 2, 3, 5, and the distances from Bitaro’s current position are 7,2,1,27, 2, 1, 2, respectively. Since the sightseeing spot 33 is the nearest sightseeing spot from Bitaro, he moves from the coordinate 77 to the coordinate 66 and visits the sightseeing spot 33.
  3. He has not yet visited the sightseeing spots 1,2,51, 2, 5, and the distances from Bitaro’s current position are 6,1,36, 1, 3, respectively. Since the sightseeing spot 22 is the nearest sightseeing spot from Bitaro, he moves from the coordinate 66 to the coordinate 55 and visits the sightseeing spot 22.
  4. He has not yet visited the sightseeing spots 1,51, 5, and the distances from Bitaro’s current position are 5,45, 4, respectively. Since the sightseeing spot 55 is the nearest sightseeing spot from Bitaro, he moves from the coordinate 55 to the coordinate 99 and visits the sightseeing spot 55.
  5. He has not yet visited the sightseeing spot 11. Since the sightseeing spot 11 is the nearest sightseeing spot from Bitaro, he moves from the coordinate 99 to the coordinate 00 and visits the sightseeing spot 11.

Since Bitaro’s total traveling distance is 1515, output 1515.

该样例满足所有子任务的限制。

【样例解释 #2】

该样例满足子任务 3,43, 4 的限制。

【数据范围】

对于所有测试数据,满足 1N,Q2×1051\le N,Q\le2\times10^50Xi1090\le X_i\le10^9Xi<Xi+1X_i<X_{i+1}0Sj1090\le S_j\le10^9,保证所有输入均为整数。

子任务编号 分值 限制
11 55 Q=1,N2×103Q=1,N\le2\times10^3
22 1010 Q=1Q=1
33 3030 Xi+1Xi100X_{i+1}-X_i\le100
44 5555