Along with the lights Andrew purchased, he has also selected a Christmas tree. Unfortunately, it appears that Andrew is not the best at picking trees as his tree starts to deteriorate when he tries to decorate it, each branch snapping one by one. Andrew decides to not let this bring down his holiday cheer and copes with the horrible situation by turning it into a algorithmic puzzle. Since Andrew is quite an extreme man, he likes to observe extreme values. Each of the nodes in his tree is associated with a value . With each branch break, the tree splits into separate components. The range of a component is defined to be the difference between the maximum and minimum values of the nodes in that component. Andrew would like to know the maximum range across all components after every branch break. You, being Andrew's only friend, need to use your vast algorithmic knowledge to assist him in solving this problem.
Input Specification
The first line contains one integer , the number of nodes in Andrew's tree.
The second line contains space separated integers, , the value of the node.
The next lines each contains two integers, and , the indicating that nodes and node are connected by the branch.
The final line contains space separated integers, the order in which the branches will break.
Output Specification
Output space separated integers, the intial range of the tree before any branch breaks, as well as the maximum component range after each of the branch breaks.
Subtasks
Subtask 1 [10%]
Subtask 2 [90%]
No further constraints.
Sample Input
6
1 2 3 4 5 6
6 1
1 2
5 1
4 2
1 3
1 5 2 3 4
Sample Output
5 4 4 4 2 0
Sample Explanation
The branches break in the order , , , , and .
The inital range of the tree before any branch breaks is as the maximum value is and the minimum value is . After the first branch break which separates nodes and , the tree splits into separate components and . The range of is which is greater compared to the range of , . After the second branch break which separates nodes and , the components are now , , and . The maximum range is still . This process continues until all nodes are their own separate component.
Comments