#D2598. Tree Fragments
Tree Fragments
Tree Fragments
Problem
Ai-chan has a tree consisting of vertices and edges. Each vertex has a number from to and a positive integer weight.
Answer the following queries in order.
- () is given, so you can remove the vertices on the - path of and the edges that connect to them. Among the connected components, the weight of the connected component having the maximum weight is output. If there is no such connected component, 0 is output.
However, the weight of the connected component is defined by the sum of the weights of the vertices included in the connected component.
Ouput
For each query, 0 or the weight of the connected component with the maximum weight is output on one line.
Constraints
The input satisfies the following conditions.
- ()
- ()
- is and
- ()
Input
The input is given in the following format.
... ... ...
All inputs are given as integers.
The number of vertices of is given in the first line. On the second line, the weight of the vertex () is given, separated by blanks. The end points of the edge () are given in the third and subsequent lines of -1 separated by blanks. The number of queries is given on the second line of +. representing the query () are given in the line after the + 3rd line, separated by blanks.
Examples
Input
10 1 2 3 4 5 6 7 8 9 10 1 2 2 3 3 4 3 5 2 6 6 7 2 8 8 9 8 10 3 3 8 7 1 7 10
Output
13 27 12
Input
5 1 2 3 4 5 1 2 2 3 3 4 4 5 3 1 2 1 5 2 4
Output
12 0 5
Input
15 3 8 4 2 6 5 1 2 10 3 4 2 6 1 1 1 2 2 3 3 4 3 5 2 6 6 7 6 8 1 9 9 10 10 11 10 12 9 13 13 14 13 15 5 1 1 2 7 6 10 1 15 3 4
Output
28 30 12 28 46
inputFormat
outputFormat
output. If there is no such connected component, 0 is output.
However, the weight of the connected component is defined by the sum of the weights of the vertices included in the connected component.
Ouput
For each query, 0 or the weight of the connected component with the maximum weight is output on one line.
Constraints
The input satisfies the following conditions.
- ()
- ()
- is and
- ()
Input
The input is given in the following format.
... ... ...
All inputs are given as integers.
The number of vertices of is given in the first line. On the second line, the weight of the vertex () is given, separated by blanks. The end points of the edge () are given in the third and subsequent lines of -1 separated by blanks. The number of queries is given on the second line of +. representing the query () are given in the line after the + 3rd line, separated by blanks.
Examples
Input
10 1 2 3 4 5 6 7 8 9 10 1 2 2 3 3 4 3 5 2 6 6 7 2 8 8 9 8 10 3 3 8 7 1 7 10
Output
13 27 12
Input
5 1 2 3 4 5 1 2 2 3 3 4 4 5 3 1 2 1 5 2 4
Output
12 0 5
Input
15 3 8 4 2 6 5 1 2 10 3 4 2 6 1 1 1 2 2 3 3 4 3 5 2 6 6 7 6 8 1 9 9 10 10 11 10 12 9 13 13 14 13 15 5 1 1 2 7 6 10 1 15 3 4
Output
28 30 12 28 46
样例
10
1 2 3 4 5 6 7 8 9 10
1 2
2 3
3 4
3 5
2 6
6 7
2 8
8 9
8 10
3
3 8
7 1
7 10
13
27
12
</p>