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 PYTHON3):

Solution:
# -*- coding: utf-8 -*-

# Based on the following blog.
# https://null-mn.hatenablog.com/entry/2020/04/14/124151

from collections import defaultdict

class ReRooting:
    def __init__(self, f, g, merge, ie):
        self.tree = defaultdict(list)
        self.f = f
        self.g = g
        self.merge = merge
        self.ie = ie
        self.dp = defaultdict(dict)

    def add_edge(self, u, v):
        self.tree[u].append(v)
        self.tree[v].append(u)

    def __dfs1(self, u, p):
        o = []
        s = [(u, -1)]
        while s:
            u, p = s.pop()
            o.append((u, p))
            for v in self.tree[u]:
                if v == p:
                    continue
                s.append((v, u))

        for u, p in reversed(o):
            r = self.ie
            for v in self.tree[u]:
                if v == p:
                    continue
                r = self.merge(r, self.f(self.dp[u][v], v))

            self.dp[p][u] = self.g(r, u)

    def __dfs2(self, u, p, a):
        s = [(u, p, a)]

        while s:
            u, p, a = s.pop()
            self.dp[u][p] = a

            pl = [0] * (len(self.tree[u]) + 1)
            pl[0] = self.ie
            for i, v in enumerate(self.tree[u]):
                pl[i+1] = self.merge(pl[i], self.f(self.dp[u][v], v))

            pr = [0] * (len(self.tree[u]) + 1)
            pr[-1] = self.ie
            for i, v in reversed(list(enumerate(self.tree[u]))):
                pr[i] = self.merge(pr[i+1], self.f(self.dp[u][v], v))

            for i, v in enumerate(self.tree[u]):
                if v == p:
                    continue
                r = self.merge(pl[i], pr[i+1])
                s.append((v, u, self.g(r, u)))

    # def __dfs1(self, u, p):
    #     r = self.ie
    #     for v in self.tree[u]:
    #         if v == p:
    #             continue
    #         self.dp[u][v] = self.__dfs1(v, u)
    #         r = self.merge(r, self.f(self.dp[u][v], v))

    #     return self.g(r, u)

    # def __dfs2(self, u, p, a):
    #     for v in self.tree[u]:
    #         if v == p:
    #             self.dp[u][v] = a
    #             break
    #     pl = [0] * (len(self.tree[u]) + 1)
    #     pl[0] = self.ie
    #     for i, v in enumerate(self.tree[u]):
    #         pl[i+1] = self.merge(pl[i], self.f(self.dp[u][v], v))

    #     pr = [0] * (len(self.tree[u]) + 1)
    #     pr[-1] = self.ie
    #     for i, v in reversed(list(enumerate(self.tree[u]))):
    #         pr[i] = self.merge(pr[i+1], self.f(self.dp[u][v], v))

    #     for i, v in enumerate(self.tree[u]):
    #         if v == p:
    #             continue
    #         r = self.merge(pl[i], pr[i+1])
    #         self.__dfs2(v, u, self.g(r, v))

    def build(self, root=1):
        self.__dfs1(root, -1)
        self.__dfs2(root, -1, self.ie)

    def slv(self, u):
        r = self.ie
        for v in self.tree[u]:
            r = self.merge(r, self.f(self.dp[u][v], v))

        return self.g(r, u)


def atcoder_dp_dp_v():
    # https://atcoder.jp/contests/dp/tasks/dp_v
    # https://atcoder.jp/contests/dp/submissions/16667541
    N, M = map(int, input().split())
    XY = [list(map(int, input().split())) for _ in range(N-1)]

    f = lambda x, y: x
    g = lambda x, y: x + 1
    merge = lambda x, y: (x * y) % M
    rr = ReRooting(f, g, merge, 1)
    for x, y in XY:
        rr.add_edge(x, y)

    rr.build()
    for u in range(1, N+1):
        print(rr.slv(u)-1)


def atcoder_abc60_f():
    # https://atcoder.jp/contests/abc160/tasks/abc160_f
    # https://atcoder.jp/contests/abc160/submissions/16667517
    N = int(input())
    AB = [list(map(int, input().split())) for _ in range(N-1)]

    M = 10**9+7

    from functools import reduce

    fact = [1]
    for i in range(1, 10**6):
        fact.append(fact[-1]*i % M)

    def f(x, _):
        c = x[0]
        s = x[1]
        c *= pow(fact[s], M-2, M)
        c %= M
        return (c, s)

    def g(x, _):
        c = x[0]
        s = x[1]
        c *= fact[s]
        c %= M
        return (c, s+1)

    def merge(x, y):
        return (x[0]*y[0]%M, x[1]+y[1])

    rr = ReRooting(f, g, merge, (1, 0))
    for a, b in AB:
        rr.add_edge(a, b)

    rr.build()
    for u in range(1, N+1):
        print(rr.slv(u)[0])


def codeforces_1324_f():
    import sys
    input = sys.stdin.buffer.readline
    from operator import add
    N = int(input())
    A = list(map(int, input().split()))
    UV = [list(map(int, input().split())) for _ in range(N-1)]

    A = [1 if A[i] == 1 else -1 for i in range(N)]

    def f(x, _):
        return max(0, x)

    def g(x, v):
        return x + A[v-1]

    rr = ReRooting(f, g, add, 0)
    for u, v in UV:
        rr.add_edge(u, v)

    rr.build()
    ans = []
    for u in range(1, N+1):
        ans.append(rr.slv(u))

    print(*ans)


if __name__ == '__main__':
    # atcoder_dp_dp_v()
    # atcoder_abc60_f()
    codeforces_1324_f()