Problem: You are given a tree consisting of n vertices. A tree is a connected undirected graph with n-1 edges. Each vertex v of this tree has a color assigned to it (a_v = 1 if the vertex v is white and 0 if the vertex v is black).

You have to solve the following problem for each vertex v: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex v? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cnt_w white vertices and cnt_b black vertices, you have to maximize cnt_w - cnt_b.

Input

The first line of the input contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the number of vertices in the tree.

The second line of the input contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 1), where a_i is the color of the i-th vertex.

Each of the next n-1 lines describes an edge of the tree. Edge i is denoted by two integers u_i and v_i, the labels of vertices it connects (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i).

It is guaranteed that the given edges form a tree.

Output

Print n integers res_1, res_2, ..., res_n, where res_i is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex i.

Examples

Input


9
0 1 1 1 0 0 0 0 1
1 2
1 3
3 4
3 5
2 6
4 7
6 8
5 9


Output


2 2 2 2 2 1 1 0 2 


Input


4
0 0 1 0
1 2
1 3
1 4


Output


0 -1 1 -1 

Note

The first example is shown below:

<image>

The black vertices have bold borders.

In the second example, the best subtree for vertices 2, 3 and 4 are vertices 2, 3 and 4 correspondingly. And the best subtree for the vertex 1 is the subtree consisting of vertices 1 and 3.

Solution (in JAVA):

Solution:
import java.io.OutputStream;
import java.io.InputStream;
import java.util.Random;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.ArrayList;


/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author sarthakmanna
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        FTreeWithMaximumCost solver = new FTreeWithMaximumCost();
        solver.solve(1, in, out);
        out.close();
    }

    static class FTreeWithMaximumCost {
        final static int MAXN = 1000_006;
        final static long MOD = 1000_000_007;
        final static Helper hp = new Helper(MOD, MAXN);
        long ans;
        long totalSum;
        int N;
        long[] A;
        ArrayList<Integer>[] graph;
        long[] memV;
        long[] memC;

        public void solve(int testNumber, FastReader in, PrintWriter out) {
            int i, j, k;

            N = in.nextInt();
            A = in.getLongArray(N);
            graph = new ArrayList[N];
            for (i = 0; i < N; ++i) graph[i] = new ArrayList<>();
            for (i = 1; i < N; ++i) {
                int a = in.nextInt() - 1, b = in.nextInt() - 1;
                graph[a].add(b);
                graph[b].add(a);
            }

            totalSum = hp.sum(A);
            ans = 0;
            memC = new long[N];
            memV = new long[N];
            rootAt(0, -7, 0, 0);
            out.println(ans);
        }

        void rootAt(int node, int par, long valSum, long costSum) {
            long subCostSum = getCost(node, par);
            ans = Math.max(ans, costSum + subCostSum);
            for (int itr : graph[node])
                if (itr != par) {
                    long tempCost = costSum + subCostSum - getCost(itr, node)
                            - getVal(itr, node) + totalSum - getVal(itr, node);
                    rootAt(itr, node, valSum + A[node], tempCost);
                }
        }

        long getVal(int node, int par) {
            if (memV[node] > 0) return memV[node];

            long ret = A[node];
            for (int itr : graph[node])
                if (itr != par) {
                    ret += getVal(itr, node);
                }
            return memV[node] = ret;
        }

        long getCost(int node, int par) {
            if (memC[node] > 0) return memC[node];

            long ret = 0;
            for (int itr : graph[node])
                if (itr != par) {
                    ret += getCost(itr, node) + getVal(itr, node);
                }
            return memC[node] = ret;
        }

    }

    static class FastReader {
        static final int BUFSIZE = 1 << 20;
        static byte[] buf;
        static int index;
        static int total;
        static InputStream in;

        public FastReader(InputStream is) {
            try {
                in = is;
                buf = new byte[BUFSIZE];
            } catch (Exception e) {
            }
        }

        private int scan() {
            try {
                if (index >= total) {
                    index = 0;
                    total = in.read(buf);
                    if (total <= 0)
                        return -1;
                }
                return buf[index++];
            } catch (Exception | Error e) {
                System.err.println(e.getMessage());
                return 13 / 0;
            }
        }

        public int nextInt() {
            int c, val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+')
                c = scan();
            for (; c >= '0' && c <= '9'; c = scan())
                val = (val << 3) + (val << 1) + (c & 15);
            return neg ? -val : val;
        }

        public long nextLong() {
            int c;
            long val = 0;
            for (c = scan(); c <= 32; c = scan()) ;
            boolean neg = c == '-';
            if (c == '-' || c == '+')
                c = scan();
            for (; c >= '0' && c <= '9'; c = scan())
                val = (val << 3) + (val << 1) + (c & 15);
            return neg ? -val : val;
        }

        public long[] getLongArray(int size) {
            long[] ar = new long[size];
            for (int i = 0; i < size; ++i) ar[i] = nextLong();
            return ar;
        }

    }

    static class Helper {
        final long MOD;
        final int MAXN;
        final Random rnd;

        public Helper() {
            MOD = 1000_000_007;
            MAXN = 1000_006;
            rnd = new Random();
        }

        public Helper(long mod, int maxn) {
            MOD = mod;
            MAXN = maxn;
            rnd = new Random();
        }

        public long sum(long... ar) {
            long sum = 0;
            for (long itr : ar) sum += itr;
            return sum;
        }

    }
}