#P1605E. Array Equalizer

    ID: 250 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>binary searchgreedyimplementationmathnumber theorysortingstwo pointers*2400

Array Equalizer

No submission language available for this problem.

Description

Jeevan has two arrays aa and bb of size nn. He is fond of performing weird operations on arrays. This time, he comes up with two types of operations:

  • Choose any ii (1in1 \le i \le n) and increment aja_j by 11 for every jj which is a multiple of ii and 1jn1 \le j \le n.
  • Choose any ii (1in1 \le i \le n) and decrement aja_j by 11 for every jj which is a multiple of ii and 1jn1 \le j \le n.

He wants to convert array aa into an array bb using the minimum total number of operations. However, Jeevan seems to have forgotten the value of b1b_1. So he makes some guesses. He will ask you qq questions corresponding to his qq guesses, the ii-th of which is of the form:

  • If b1=xib_1 = x_i, what is the minimum number of operations required to convert aa to bb?

Help him by answering each question.

The first line contains a single integer nn (1n2105)(1 \le n \le 2 \cdot 10^{5})  — the size of arrays aa and bb.

The second line contains nn integers a1,a2,...,ana_1, a_2, ..., a_n (1ai106)(1 \le a_i \le 10^6).

The third line contains nn integers b1,b2,...,bnb_1, b_2, ..., b_n (1bi106(1 \le b_i \le 10^6 for i1i \neq 1; b1=1b_1 = -1, representing that the value of b1b_1 is unknown)).

The fourth line contains a single integer qq (1q2105)(1 \le q \le 2 \cdot 10^{5})  — the number of questions.

Each of the following qq lines contains a single integer xix_i (1xi106)(1 \le x_i \le 10^6)  — representing the ii-th question.

Output qq integers  — the answers to each of his qq questions.

Input

The first line contains a single integer nn (1n2105)(1 \le n \le 2 \cdot 10^{5})  — the size of arrays aa and bb.

The second line contains nn integers a1,a2,...,ana_1, a_2, ..., a_n (1ai106)(1 \le a_i \le 10^6).

The third line contains nn integers b1,b2,...,bnb_1, b_2, ..., b_n (1bi106(1 \le b_i \le 10^6 for i1i \neq 1; b1=1b_1 = -1, representing that the value of b1b_1 is unknown)).

The fourth line contains a single integer qq (1q2105)(1 \le q \le 2 \cdot 10^{5})  — the number of questions.

Each of the following qq lines contains a single integer xix_i (1xi106)(1 \le x_i \le 10^6)  — representing the ii-th question.

Output

Output qq integers  — the answers to each of his qq questions.

Samples

Sample Input 1

2
3 7
-1 5
3
1
4
3

Sample Output 1

2
4
2

Sample Input 2

6
2 5 4 1 3 6
-1 4 6 2 3 5
3
1
8
4

Sample Output 2

10
29
9

Note

Consider the first test case.

  • b1=1b_1 = 1: We need to convert [3,7][1,5][3, 7] \rightarrow [1, 5]. We can perform the following operations:

    [3,7][3, 7] decreasei = 1\xrightarrow[\text{decrease}]{\text{i = 1}} [2,6][2, 6] decreasei = 1\xrightarrow[\text{decrease}]{\text{i = 1}} [1,5][1, 5]

    Hence the answer is 22.

  • b1=4b_1 = 4: We need to convert [3,7][4,5][3, 7] \rightarrow [4, 5]. We can perform the following operations:

    [3,7][3, 7] decreasei = 2\xrightarrow[\text{decrease}]{\text{i = 2}} [3,6][3, 6] decreasei = 2\xrightarrow[\text{decrease}]{\text{i = 2}} [3,5][3, 5] increasei = 1\xrightarrow[\text{increase}]{\text{i = 1}} [4,6][4, 6] decreasei = 2\xrightarrow[\text{decrease}]{\text{i = 2}} [4,5][4, 5]

    Hence the answer is 44.

  • b1=3b_1 = 3: We need to convert [3,7][3,5][3, 7] \rightarrow [3, 5]. We can perform the following operations:

    [3,7][3, 7] decreasei = 2\xrightarrow[\text{decrease}]{\text{i = 2}} [3,6][3, 6] decreasei = 2\xrightarrow[\text{decrease}]{\text{i = 2}} [3,5][3, 5]

    Hence the answer is 22.