#C4947. Subtree Sums in a Tree

    ID: 48541 Type: Default 1000ms 256MiB

Subtree Sums in a Tree

Subtree Sums in a Tree

Given a tree with \(n\) nodes where each node \(i\) has an associated value \(v_i\), you are required to answer \(q\) queries. In each query, you are given a node \(u\) and you must compute the sum of all values in the subtree rooted at \(u\). The subtree of \(u\) is defined as \(S(u) = v(u) + \sum_{w \in \text{child}(u)} S(w)\), where the tree is considered to be rooted at node 1.

Note: The tree is given as an undirected graph but you should assume it is rooted at node 1. For each query, the subtree sum of \(u\) is the sum of \(v_i\) for every node \(i\) in the subtree of \(u\) when the tree is rooted at 1.

inputFormat

The first line contains two integers \(n\) and \(q\), where \(n\) is the number of nodes in the tree and \(q\) is the number of queries.

The second line contains \(n\) integers, \(v_1, v_2, \ldots, v_n\), representing the values associated with each node (nodes are labeled from 1 to \(n\)).

The following \(n-1\) lines each contain two integers \(u\) and \(v\), representing an undirected edge between nodes \(u\) and \(v\).

The next \(q\) lines each contain one integer, representing the query node.

outputFormat

For each query, print a single line containing one integer: the sum of the values in the subtree rooted at the queried node.

## sample
5 3
1 2 3 4 5
1 2
1 3
2 4
2 5
2
3
1
11

3 15

</p>