Elephants enjoy taking leaves from trees to eat. At the i
-th node of the tree, there are vi
leaves.
However, trees in computer science differ from real-world trees, the tree has n
nodes and n - 1
edges and is rooted at node 1
.
If a node is removed, all nodes beneath it (i.e., its subtree) will also be removed.
The problem involves q
queries, and each query provides an integer k
, asking how many leaves in total (including the leaves at node k
) will be removed if node k
is removed.
(In this problem, "leaves" are used to represent the value of a node, not the actual leaf nodes as defined in a tree structure.)
The first line contains two integers, n
and q
.
2 ≤ n
, q
≤ 1000 (Testcases 1 ~ 2)
2 ≤ n
, q
≤ 2 * 105 (Testcases 3 ~ 5)
The second line contain n
integers vi
, representing the leaves on i
-th node.
1 ≤ vi
≤ 1000
The following n - 1
lines each line contain two integers u
and v
, representing that u
and v
is connected.
1 ≤ u
, v
≤ n
It is guaranteed that the graph is a tree.
The following q
lines each line contain a integer k
, answer the sum of leaves on the k
-th node's subtree.
1 ≤ k
≤ n
For each query, output the answer of sum of leaves on the k
-th node's subtree, followed by a new line character '\n'
.