Submit solution

Points: 20
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

The University of Waterloo is in major disarray. There are too many geese. The sheer amount of geese has made navigating around the campus nearly impossible. That's why Joe is developing a sophisticated piece of software that can help students navigate the campus!

The campus is represented by a cluster of N islands, each with one single-directional bridge that goes to another island. On each of these islands, stands a goose. Initially, each island will be occupied by a goose, but after t_i seconds, the goose on the i^{th} island will fly away into the horizon.

Every morning, each student q starts on island v_q and walks precisely b_q islands. It can be proven that there exists only one path that fits this description. Joe's software must be designed to help students navigate across these islands without encountering any geese. It must calculate the earliest time a student should start walking while avoiding all geese on their way. Note, a student walks strictly across one bridge per second, thus the software is able to optimize the starting time by including the time it takes for the student to walk to any given island. Once a student starts walking, they cannot stop to wait for a goose to fly away. It is acceptable for a goose will fly away during or before the moment the student walks on the island.

Since the students are curious about this new software, all K of them have submitted a query! Joe, however, does not know how to handle this many queries, so he asked you to help him!

Input Specification

The first line of input contains two integers N and K (1 \le N, K \le 100\,000).

The next line will contain N space-separated integers t_i (0 \le t_i \le 10^{9}), representing the time for the goose on island i to take off.

On the next line, there will again contain N space-separated integers d_i, representing the island that the outgoing bridge leads to.

The following K lines will each contain a query q of b_q and v_q, (0 \le b_q \le 10^{9}; 1 \le v_q \le N) representing the number of bridges they have to cross and the starting bridge, respectively.

Output Specification

For each of the K queries, output the minimum time the student must wait in order for them to walk without encountering any geese along the way. Assume that each query starts at the 0^{th} second.

Subtasks

Subtask 1 [5%]

1\leq N, K \leq 1000

0\leq t_i, b_q \leq 1000

Subtask 2 [95%]

No further restrictions

Sample Input

8 4
0 1 2 3 4 9 11 3
2 3 4 5 2 7 6 5
1000 1
4 2
4 8
1 6

Sample Output

0
1
3
10

Explanation

Explanation

The nodes are representing the islands, and are shown in a node:t_i format

Query 1: The student doesn't have to wait to start. The goose on the island 1 has flown away at the 0^{th} second, (and similarly for islands 3, 4, 5 when the student reach them) thus the time to wait is \text{zero}.

Query 2: Similar to query 1, the geese flew off before the student reaches the islands. However, they have to first wait 1 second for the goose on island 2 to clear off.

Query 3: The student must wait 3 seconds to start on island 8, and then afterwards, all the geese have either already flown off, or flew off when they reached the island.

Query 4: The student starts on island 6, but needs to wait 9 seconds to walk on it. Then, afterwards, they need to wait an additional 1 second for the goose on island 7 to take off when they reach it. This totals to 10 seconds.


Comments

There are no comments at the moment.