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.*;
import java.util.*;

public class Q1 {

    static void dfs(int graph[][], int dp[], int u, int p, int col[]) {
        int x= col[u];
        for (int val : graph[u]) {
            if (val!= p) {
                dfs(graph, dp, val, u, col);
                x += Math.max(0, dp[val]);
            }
        }
        dp[u]=x;
    }
    static void dfs2(int graph[][], int dp[], int u, int p, int col[],int ans[]) {
        ans[u]=dp[u];
        if(p!=-1){
            int val=ans[p]-Math.max(0,dp[u]);
            ans[u]+=Math.max(0,val);
        }
        for (int val : graph[u]) {
            if (val!= p)
                dfs2(graph, dp, val, u, col,ans);

        }
    }

    public static void main(String[] args) {

        InputReader in = new InputReader();
        PrintWriter out = new PrintWriter(System.out);

        int N = in.nextInt();
        int arr[] = new int[N];
        for (int i = 0; i < N; i++) arr[i] = in.nextInt() == 1 ? 1 : -1;
        int from[] = new int[N - 1], to[] = new int[N - 1];
        for (int i = 0; i < N - 1; i++) {
            from[i] = in.nextInt()-1; to[i] = in.nextInt()-1;
        }
        int graph[][] = packU(N, from, to);
        int below[] = new int[N],above[]=new int[N];
        dfs(graph, below, 0, -1, arr);
        dfs2(graph, below, 0, -1, arr,above);

        for (int i = 0; i < below.length; i++) {
            System.out.print( above[i]+ " ");
        }


        out.close();
    }


    //    public static void main(String[] args) {
//
//        InputReader in = new InputReader();
//        PrintWriter out = new PrintWriter(System.out);
//        int t = in.nextInt();
//        while (t-- > 0) {
//
//            int N = in.nextInt(), max = -1;
//            int arr[] = new int[N];
//            for (int i = 0; i < N; i++) {
//                arr[i] = in.nextInt();
//                max = Math.max(max, arr[i]);
//            }
//            if (max == -1) {
//                out.println(0 + " " + 1);
//                continue;
//            }
//            int diff=Integer.MAX_VALUE;
//
//            int l=0,r=(int)1e9;
//            while(l<=r){
//                int m=(r-l+1)/3,d1=0,d2=0,m1=l+m,m2=r-m;
//                for(int i =1;i<N;i++){
//                    int  a =arr[i]==-1?m1:arr[i],b =arr[i-1]==-1?m1:arr[i-1],d=Math.abs(a-b);
//                    d1=Math.max(d1,d);
//                }
//                for(int i =1;i<N;i++){
//                    int  a =arr[i]==-1?m2:arr[i],b =arr[i-1]==-1?m2:arr[i-1],d=Math.abs(a-b);
//                    d2=Math.max(d2,d);
//                }
//                if(d1<=d2){
//                    r=m2-1;
//                    if(d1<diff){
//                       max=m1; diff=d1;}
//                }else{
//                    l=m1+1;
//                    if(d2<diff){
//                       max=m2; diff=d2;}
//                }
//            }
//
//            out.println(diff + " " + max);
//
//        }
//        out.close();
//
//
//    }
    static int[][] packU(int n, int[] from, int[] to) {
        int[][] g = new int[n][];
        int[] p = new int[n];
        for (int f : from)
            p[f]++;
        for (int t : to)
            p[t]++;
        for (int i = 0; i < n; i++)
            g[i] = new int[p[i]];
        for (int i = 0; i < from.length; i++) {
            g[from[i]][--p[from[i]]] = to[i];
            g[to[i]][--p[to[i]]] = from[i];
        }
        return g;
    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public int[] Int_Array(int s) {
            int arr[] = new int[s];
            for (int i = 0; i < s; i++)
                arr[i] = nextInt();
            return arr;

        }

        public int[] shuffle(int[] arr) {
            Random r = new Random();
            for (int i = 1, j; i < arr.length; i++) {
                j = r.nextInt(i);
                arr[i] = arr[i] ^ arr[j];
                arr[j] = arr[i] ^ arr[j];
                arr[i] = arr[i] ^ arr[j];
            }
            return arr;
        }

        public InputReader() {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(System.in), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public char nextChar() {
            return next().charAt(0);
        }

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

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

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


    }

}


//    public static void main(String[] args) {
//
//        InputReader in = new InputReader();
//        PrintWriter out = new PrintWriter(System.out);
//        int t  =in.nextInt();
//
//        while(t-->0){
//            int  N=in.nextInt();
//            int arr[]=new int[N];
//            for(int i=0 ;i<N;i++)
//                arr[i]=in.nextInt();
//
//            ArrayList<Integer> set1=new ArrayList<>(),set2=new ArrayList<>(),set3=new ArrayList<>();
//            double xval=in.nextInt(),x=in.nextInt(),yval=in.nextInt(),y=in.nextInt();
//            long k=in.nextLong();
//            long  ans  =0;
//
//            if(xval<yval){
//                double temp=xval;
//                xval=yval;
//                yval=temp;
//
//                temp=x;
//                x=y;
//                y=temp;
//            }
//            xval=xval/100; yval=yval/100;
//
//            for(int i =0;i<N;i++){
//                if((i+1)%x==0 && (i+1)%y==0)
//                    set1.add(i);
//                else if ((i+1)%x==0 )
//                    set2.add(i);
//                else if((i+1)%y==0)
//                    set3.add(i);
//            }
//            arr=in.shuffle(arr);
//
//            int a =0,b=0,c=0,add=0;
//
//            for(int  i=N-1;i>=0;i--){
//
//                int temp=0;
//
//                if(a==set1.size()  && b==set2.size()  && c==set3.size())
//                    continue;
//                else if (a==set1.size()  && b==set2.size() ){
//                    ans += (yval) * arr[i];
//                    c++;
//                    temp=11;
//                }else if(a==set1.size()){
//                    if (set2.get(b) <set3.get(c)) {
//                        ans += (xval) * arr[i];
//                        b++;
//                    } else {
//                        ans += (yval) * arr[i];
//                        c++;
//                    }
//                }else{
//                    if (set1.get(a) < set2.get(b) && set1.get(a) < set3.get(c)) {
//                        ans += (xval + yval) * arr[i];
//                        a++;
//                    } else if (set2.get(b)<set3.get(c)) {
//                        ans += (xval) * arr[i];
//                        b++;
//                    } else {
//                        ans += (yval) * arr[i];
//                        c--;
//                    }
//                }
//
//
//
//                if(ans>=k){
//                    break;
//                }
//
//            }
//
//            if(ans<k)
//                out.println(-1);
//            else
//                out.println(add);
//
//
//
//        }
//
//        out.close();
//    }