G: Restricted DFS
Problem
Given an undirected tree G with N vertices and N-1 edges, without self-loops or multiple edges. Each vertex is labeled with a non-negative integer A_i, and the vertices are numbered from 1 to N. The edges are also numbered from 1 to N-1, and the ith edge connects vertices u_i and v_i.
Consider performing depth-first search (DFS) on this tree, starting from the root r and following the pseudocode below.
// [input]
// G: The graph to be searched by DFS
// A: Non-negative integers assigned to each vertex
// v: The vertex to start DFS from
// step: An integer that records the number of steps taken
// [output]
// One of the following two values:
// - SUCCESS: DFS returns to vertex v without stopping in the middle
// - FAILURE: DFS stops in the middle
function dfs(G, A, v, step)
    if (A[v] is 0) then
        return FAILURE
    A[v] ← A[v] - 1
    step ← step + 1
    Sort the children of v in ascending order of their vertex numbers
    // c is visited in ascending order of vertex numbers
    for each (child c of v) do
        if (dfs(G, A, c, step) is FAILURE) then
            return FAILURE
        if (A[v] is 0) then
            return FAILURE
        A[v] ← A[v] - 1
        step ← step + 1
    return SUCCESS
That is, given G and A, execute
dfs(G, A, r, 0)
We want to find the number of steps taken by this DFS when each vertex is taken as the root.
Input
N
A_1 ... A_N
u_1 v_1
...
u_{N-1} v_{N-1}
 The first line contains the number of vertices N of the graph.
 The second line contains N integers. The ith integer A_i represents the value on the ith vertex.
 The next N-1 lines contain information about the edges of the graph. Each line contains two integers u_i and v_i, indicating that there is an undirected edge between vertex u_i and vertex v_i.
Constraints
 1 \leq N \leq 3 \times 10^5
 0 \leq A_i \leq 10^9
 1 \leq u_i < v_i \leq N
 The graph given is guaranteed to be a tree.
 All input is given as integers.
Output
Output N lines. The ith line should contain the number of steps taken in the DFS when vertex i is used as the root.
Sample Input 1
3
1 2 3
1 2
1 3
Sample Output 1
2
3
3
 When vertex 1 is used as the root