| TeamsCode Summer 2026 Contest |
|---|
| Finished |
You are given a tree with $$$n$$$ nodes, where each node has a strength of $$$a_i$$$.
You are asked to perform $$$q$$$ surgeries on the tree. Each surgery has an intensity of $$$k$$$. A surgery can be successfully performed if and only if it is possible to remove a (possibly empty) set of edges from the tree such that the strength of every resulting connected component is exactly $$$k$$$.
The strength of a connected component is defined as the sum of the strengths of all nodes within it.
For each of the $$$q$$$ surgeries, determine whether it can be performed. Note that the queries are independent; the tree is completely restored to its original state after evaluating each query.
The first line contains one integer, $$$n \text{ } (1 \leq n \leq 10^6)$$$ — the number of nodes in the tree.
The next line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n \text{ }(0 \leq a_i \leq 10^{12})$$$ — the strength of each node on the tree.
Each of the next $$$n - 1$$$ lines contains two integers, $$$u \text{ } (1 \leq u \leq n)$$$ and $$$v \text{ }(1 \leq v \leq n)$$$, representing an edge connecting node $$$u$$$ and node $$$v$$$.
The next line contains one integer, $$$q \text{ } (1 \leq q \leq 10^6)$$$ — the number of surgeries you need to perform.
Each of the next $$$q$$$ lines contains one integer, $$$k \text{ } (0 \leq k \leq 10^{18})$$$ — the intensity of the surgery.
—
Tests $$$1$$$ — $$$4$$$ satisfy $$$n, q \leq 2000$$$.
Tests $$$5$$$ — $$$10$$$ satisfy $$$a_i \geq 1$$$.
Tests $$$11$$$ — $$$20$$$ satisfy no additional constraints.
For each surgery, output YES if it can be performed, and NO otherwise.
32 0 01 21 311
NO
80 4 2 2 4 0 4 41 21 33 41 51 66 76 8641052028
YES NO NO YES NO NO
For the first sample, the total strength of the tree is $$$2$$$.
For the second sample, the total strength of the tree is $$$20$$$.
| Name |
|---|


