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.math.*;
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.*; 
import java.util.StringTokenizer; 

public class Main 
{ 
    static int MAXN=201;
    static int spf[] = new int[MAXN]; 
    static int ans[]=new int[MAXN];
    static int id[]=new int[MAXN];
    static int sz[]=new int[MAXN];
    static HashSet s=new HashSet();
	static class FastReader 
	{ 
		BufferedReader br; 
		StringTokenizer st; 

		public FastReader() 
		{ 
			br = new BufferedReader(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; 
		} 
	} 
    static void sieves() 
    { 
        spf[1] = 1; 
        for (int i=2; i<MAXN; i++) 
            spf[i] = i; 
        for (int i=4; i<MAXN; i+=2) 
            spf[i] = 2; 
        for (long i=3; i*i<MAXN; i++) 
        { 
            if (spf[(int)i] == i) 
            { 
                for (long j=i*i; j<MAXN; j+=i) 
                    if (spf[(int)j]==j) 
                        spf[(int)j] = (int)i; 
            } 
        } 
    } 
    static void getFactorizations(int x) 
    { 
        Vector<Integer> ret = new Vector<>(); 
        HashSet w=new HashSet();
        while (x != 1 && spf[x]!=1) 
        { 
            if(!w.contains(spf[x]) && spf[x]!=1)
            {
            w.add(spf[x]);
            ans[spf[x]]++; 
            }
            x = x / spf[x]; 
        }
    }
    public static void indfs(int src,ArrayList<Integer>adj[],int visit[],int col[],int dp[][]){
        visit[src]=1;
        dp[src][col[src]]++;
        for(int i:adj[src]){
            if(visit[i]==0){
                indfs(i,adj,visit,col,dp);
                if(dp[i][1]>dp[i][0]){
                    dp[src][0]+=dp[i][0];
                    dp[src][1]+=dp[i][1];
                }
            }
        }
    }
    public static void outdfs(int src,ArrayList<Integer>adj[],int visit[],int col[],int dp[][]){
        visit[src]=1;
        for(int i:adj[src]){
            if(visit[i]==0){
                //System.out.println(src+" wht="+dp[src][1]+" blk="+dp[src][0]);
                if(dp[src][1]>dp[src][0]){
                    int wht=dp[src][1],blk=dp[src][0];
                    //System.out.println(i+" wht="+dp[i][1]+" blk="+dp[i][0]+" child of "+src);
                    if(dp[i][1]>dp[i][0]){
                        wht=dp[src][1]-dp[i][1];
                        blk=dp[src][0]-dp[i][0];
                    }
                    if(wht>blk){
                        dp[i][1]+=wht;
                        dp[i][0]+=blk;
                    }
                }
                outdfs(i,adj,visit,col,dp);
            }
        }
    }
	public static void main(String[] args) 
	{ 
		FastReader sc=new FastReader();
		int T=1;
		while(T-->0){
	        int N=sc.nextInt();
	        int col[]=new int[N];
	        int blk=0,wht=0;
	        for(int i=0;i<N;i++){
	            col[i]=sc.nextInt();
	        }
	        ArrayList<Integer>adj[]=new ArrayList[N];
	        for(int i=0;i<N;i++)adj[i]=new ArrayList<Integer>();
	        for(int i=0;i<N-1;i++){
	            int u=sc.nextInt()-1;
	            int v=sc.nextInt()-1;
	            adj[u].add(v);
	            adj[v].add(u);
	        }
	        int dp[][]=new int[N][2];
	        int visit[]=new int[N];
	        indfs(0,adj,visit,col,dp);
	        Arrays.fill(visit,0);
	        outdfs(0,adj,visit,col,dp);
	        for(int i=0;i<N;i++){
	            System.out.print((dp[i][1]-dp[i][0])+" ");
	        }
		}
	}
	public static int root(int i)
    {
        while(i!=id[i])
        i=id[i];
        return i;
    }
    public static void union(int p,int q)
    {
        int i=root(p);
        int j=root(q);
        //System.out.println(i+" "+j);
        if(sz[i]<sz[j])
        {
            id[i]=j;
            sz[j]+=sz[i];
        }
        else
        {
            id[j]=i;
            sz[i]+=sz[j];
        }
        //max=Math.max(max,Math.max(sz[i],sz[j]));
    }
	public static void sieven(int n) 
    {
        boolean prime[] = new boolean[n+1]; 
        for(int i=0;i<n;i++) 
            prime[i] = true; 
        for(int p = 2; p*p <=n; p++) 
        { 
            if(prime[p] == true) 
            { 
                for(int i = p*2; i <= n; i += p) 
                    prime[i] = false; 
            } 
        } 
        for(int p=2; p<=n;p++)
        if(prime[p]==true)
        s.add(p);
    } 
	public static long gcd(long a,long b)
	{
	    if(b==0)
	    return a;
	    return gcd(b,a%b);
	}
	static long power(long x, long y, int p) 
    { 
        long res = 1;      
        x = x % p;  
        while (y > 0) 
        { 
            if((y & 1)==1) 
                res = (res * x) % p; 
            y = y >> 1;  
            x = (x * x) % p;  
        } 
        return res; 
    }
    static class Pair
	{ 
		int x; 
		int y;
		public Pair(int x, int y) 
		{	 
			this.x = x; 
			this.y = y; 
		}	 
	} 
    static class Compare
	{ 
		static void compare(Pair arr[], int n) 
		{ 
			// Comparator to sort the pair according to second element 
			Arrays.sort(arr, new Comparator<Pair>() { 
				@Override public int compare(Pair p1, Pair p2) 
				{ 
					if(p1.x>p2.x)
					{
						return 1;
					}
					else if(p2.x>p1.x)
					{
						return -1;
					}
					else
					{
						return 0;
					} 
				} 
			}); 
		} 
	}
}