spoj#MEPPERM. Maximum Edge of Powers of Permutation
Maximum Edge of Powers of Permutation
For a directed graph G where any vertex v has two weights Av and Bv, we call Au+Bv the weight of a edge (u,v). Let MaxEdge(G) be the maximum weight of the edges of G.
Given a permutation P on 1..n, we can derive a directed graph G=(V,E) where V={1,..,n} and (u,v) in E iff P(u)=v. Your task is to compute MaxEdge(Pk) for every k in 0..q-1.
Input
The first line contains a positive integer n.
The second line contains n integers in {1,..,n}, denoting the permutation P.
The third and the fourth line both contain n natural numbers, A1,..,An and B1,..,Bn respectively.
The fifth line contains a positive integer q.
Output
The only one line contains q integers MaxEdge(P0),..,MaxEdge(Pq-1), separated by a single space.
Example
Input:
3
3 2 1
0 1 2
2 2 0
5
Output:
3 4 3 4 3
Constraint
n <= 66000
Ai, Bi <= 16
q <= 106
Notice
The time limit is somehow strict. Please do not spoil the problem with a cheating solution.
Description updated on 2010-7-11