#B4187. Candy Distribution Circle

    ID: 11844 Type: Default 1000ms 256MiB

Candy Distribution Circle

Candy Distribution Circle

In this problem, there are (n) students sitting in a circle. The teacher gives a candy to the (i)-th student at time (t_i) seconds. After receiving a candy, the (i)-th student waits for (p_i) seconds and then immediately passes the candy to the next student (with the (n)-th student passing to the 1st student). A student may receive candies both from the teacher and from the previous student, and whenever a student receives a candy, they will eventually pass it on. It is guaranteed that the teacher has an unlimited supply of candies.

Jimmy is curious to know the earliest time at which each of his (k) friends can get a candy. Given the arrays (t = [t_1, t_2, \ldots, t_n]) and (p = [p_1, p_2, \ldots, p_n]), along with the list of friend indices, determine the minimum time each friend will receive a candy.

The candy receiving times for each student can be determined by considering the following recurrence in the circle: [ ans_{i} = \min\Big( t_i,; ans_{i-1} + p_{i-1} \Big) ] with the cyclic condition that (ans_1 = \min\Big( t_1,; ans_n + p_n \Big)). This propagation must be applied sufficiently many times to reach a fixed point.

inputFormat

The first line contains two integers \(n\) and \(k\) \((1 \leq n \leq 10^5,\; 1 \leq k \leq n)\) representing the number of students and the number of Jimmy's friends, respectively.

The second line contains \(n\) integers \(t_1,t_2,\ldots,t_n\) \((0 \leq t_i \leq 10^9)\), where \(t_i\) is the time (in seconds) at which the teacher gives a candy to the \(i\)-th student.

The third line contains \(n\) integers \(p_1,p_2,\ldots,p_n\) \((0 \leq p_i \leq 10^9)\), where \(p_i\) is the waiting time (in seconds) after receiving a candy before the \(i\)-th student passes it to the next student (the next student of student \(n\) is student 1).

The fourth line contains \(k\) integers representing the 1-indexed positions of Jimmy's friends.

outputFormat

Output \(k\) lines. The \(i\)-th line should contain a single integer, the earliest time (in seconds) at which the corresponding friend receives a candy.

sample

5 2
5 4 3 2 1
1 1 1 1 1
2 4
3

2

</p>