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.
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import static java.lang.Math.max;

public class C1324F_2 {
    public static void main(String[] args) {
        var scanner = new BufferedScanner();
        var writer = new PrintWriter(new BufferedOutputStream(System.out));

        var n = scanner.nextInt();
//        var n = (int) 2e5;
        var a = new int[n];
        var maxTo = new ArrayList<Map<Integer, Integer>>();
        var sumMaxTo = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
//            a[i] = 1;
            if (a[i] == 0) {
                a[i] = -1;
            }
            maxTo.add(new HashMap<>());
        }
        var t = System.nanoTime();
        for (int i = 0; i < n - 1; i++) {
            var u = scanner.nextInt() - 1;
            var v = scanner.nextInt() - 1;
//            var u = i;
//            var v = i + 1;
            int v2u = max(a[v], 0);
            maxTo.get(u).put(v, v2u); // u <-- v
//            sumMaxTo[u] += v2u;
            int u2v = max(a[u], 0);
            maxTo.get(v).put(u, u2v);
//            sumMaxTo[v] += u2v;
        }

        // 第一遍bfs，计算一个方向的值，并且记录计算拓扑顺序
        var order = new LinkedList<Integer>();
        {
            var q = new LinkedList<Integer>();
            var visited = new HashMap<Integer, Integer>();
            var queued = new HashSet<Integer>();
            // 从叶子结点开始
            for (int u = 0; u < n; u++) {
                if (maxTo.get(u).size() == 1) {
                    q.add(u);
                    visited.put(u, 0);
                    queued.add(u);
                }
            }
            while (q.size() > 0) {
                var u = q.poll();
                order.addFirst(u);
                var f = max(0, sumMaxTo[u] + a[u]); // 这个时候从v到u的还没有算到sumMaxTo[u]中。
                for (var v : maxTo.get(u).keySet()) {
                    if (queued.contains(v)) {
                        continue;
                    }
                    sumMaxTo[v] += f;
                    int already = inc(visited, v);
                    int vConnectionSize = maxTo.get(v).size();
                    if (queued.size() + 1 < n && already + 1 == vConnectionSize
                            || queued.size() + 1 == n && already == vConnectionSize) {
                        q.add(v);
                        queued.add(v);
                    }
                }
            }
        }
        // 第二遍bfs，逆序计算第二个方向的值
        {
            var calculated = new HashSet<Integer>();
            for (var u : order) {
                calculated.add(u);
                var f = sumMaxTo[u] + a[u];
                for (var v : maxTo.get(u).keySet()) {
                    if (calculated.contains(v)) {
                        continue;
                    }
                    var f1 = max(0, f - max(0, sumMaxTo[v] + a[v]));
                    sumMaxTo[v] += f1;
                }
            }
        }

//        System.out.println((System.nanoTime() - t) / 1e9);
        for (int u = 0; u < n; u++) {
            var sum = sumMaxTo[u] + a[u];
            writer.print(sum + " ");
        }

        scanner.close();
        writer.flush();
        writer.close();
    }

    private static int inc(Map<Integer, Integer> visited, Integer v) {
        return visited.compute(v, (key, val) -> val == null ? 1 : val + 1);
    }

    public static class BufferedScanner {
        BufferedReader br;
        StringTokenizer st;

        public BufferedScanner(Reader reader) {
            br = new BufferedReader(reader);
        }

        public BufferedScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        void close() {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    static long gcd(long a, long b) {
        if (a < b) {
            return gcd(b, a);
        }
        while (b > 0) {
            long tmp = b;
            b = a % b;
            a = tmp;
        }
        return a;
    }

    static long inverse(long a, long m) {
        long[] ans = extgcd(a, m);
        return ans[0] == 1 ? (ans[1] + m) % m : -1;
    }

    private static long[] extgcd(long a, long m) {
        if (m == 0) {
            return new long[]{a, 1, 0};
        } else {
            long[] ans = extgcd(m, a % m);
            long tmp = ans[1];
            ans[1] = ans[2];
            ans[2] = tmp;
            ans[2] -= ans[1] * (a / m);
            return ans;
        }
    }

    private static List<Integer> primes(double upperBound) {
        var limit = (int) Math.sqrt(upperBound);
        var isComposite = new boolean[limit + 1];
        var primes = new ArrayList<Integer>();
        for (int i = 2; i <= limit; i++) {
            if (isComposite[i]) {
                continue;
            }
            primes.add(i);
            int j = i + i;
            while (j <= limit) {
                isComposite[j] = true;
                j += i;
            }
        }
        return primes;
    }


}
