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 a{

   public static void main(String[] args) {
      MyScanner sc = new MyScanner();
      out = new PrintWriter(new BufferedOutputStream(System.out));

      int n = sc.nextInt();
      long[] value = new long[n];
      long[] cost = new long[n];
      long[] sum = new long[n];
      int[] subtreedp = new int[n];
      for(int i=0; i<n; i++) {
          value[i] = sc.nextLong();
      }
      Graph g = new Graph(n);
      for(int i=0; i<n-1; i++) {
          g.addEdge(sc.nextInt()-1, sc.nextInt()-1);
      }
      long[][] ans = g.DFS(0,value);
      cost[0]=ans[0][1];
      for(int i=0; i<n; i++) {
          sum[i] = ans[i][0];
      }
      long[] yyy = g.DFSfinal(0,sum,cost);
      for(int i=0; i<n; i++) {
          cost[i] = yyy[i];
      }
      long MAX = 0;
      for(int i=0; i<n; i++) {
          if(cost[i]>MAX) {
              MAX=cost[i];
          }
      }
      out.println(MAX);


      
      // Start writing your solution here. -------------------------------------
   
      /*
      int n      = sc.nextInt();        // read input as integer
      long k     = sc.nextLong();       // read input as long
      double d   = sc.nextDouble();     // read input as double
      String str = sc.next();           // read input as String
      String s   = sc.nextLine();       // read whole line as String

      int result = 3*n;
      out.println(result);                    // print via PrintWriter
      */

      // Stop writing your solution here. -------------------------------------
      out.close();
   }

   //-----------PrintWriter for faster output---------------------------------
   public static PrintWriter out;
      
   //-----------MyScanner class for faster input----------
   public static class MyScanner {
      BufferedReader br;
      StringTokenizer st;
 
      public MyScanner() {
         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;
      }

   }

   //--------------------------------------------------------
   // This class represents a directed graph using adjacency list 
    // representation 
    public static class Graph 
    { 
        private int V;   // No. of vertices 
        private LinkedList<Integer> adj[]; //Adjacency Lists 
    
        // Constructor 
        @SuppressWarnings("unchecked") 
        Graph(int V) 
        { 
            this.V = V; 
            adj = new LinkedList[V]; 
            for (int i=0; i<V; i++) 
                adj[i] = new LinkedList<>(); 
        } 
    
        // Function to add a directed edge into the graph 
        void addDirectedEdge(int v,int w) 
        { 
            adj[v].add(w); 
        }

        // Function to add an undirected edge into the graph 
        void addEdge(int v,int w) 
        { 
            adj[v].add(w); 
            adj[w].add(v);
        }
    
        // prints BFS traversal from a given source s 
        ArrayList<ArrayList<Integer>> BFS(int s) 
        { 
            // Mark all the vertices as not visited(By default 
            // set as false) 
            boolean[] visited = new boolean[V];
            ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();

            // Call the recursive helper function to print DFS traversal 
            for (int j=0; j<V; j++)
            {
                if (!visited[j])
                {
                    // Create a queue for BFS 
                    LinkedList<Integer> queue = new LinkedList<Integer>();
                    ArrayList<Integer> comp = new ArrayList<Integer>();
            
                    // Mark the current node as visited and enqueue it 
                    visited[s]=true; 
                    queue.add(s); 
            
                    while (queue.size() != 0) 
                    { 
                        // Dequeue a vertex from queue and print it 
                        s = queue.poll(); 
                        comp.add(s);
            
                        // Get all adjacent vertices of the dequeued vertex s 
                        // If a adjacent has not been visited, then mark it 
                        // visited and enqueue it 
                        Iterator<Integer> i = adj[s].listIterator(); 
                        while (i.hasNext()) 
                        { 
                            int n = i.next(); 
                            if (!visited[n]) 
                            { 
                                visited[n] = true; 
                                queue.add(n); 
                            } 
                        } 
                    }
                    components.add(comp);
                }
            }
            return components;
            
        } 

        long costa;

        // A function used by DFS 
        long[] DFSUtil(int v, boolean[] visited, ArrayList<Integer> arrli, long[] value, long[] depth, long[] sum) 
        { 
            // Mark the current node as visited and print it 
            visited[v] = true;
            arrli.add(v);
            costa += value[v]*depth[v];
            sum[v] += value[v];
            
    
            // Recur for all the vertices adjacent to this vertex 
            Iterator<Integer> i = adj[v].listIterator(); 
            while (i.hasNext()) 
            { 
                int n = i.next(); 
                if (!visited[n]) {
                    depth[n]=depth[v]+1;
                    DFSUtil(n, visited, arrli, value, depth, sum);
                    sum[v] += sum[n];
                }
            }
            return sum;

        } 
    
        // The function to do DFS traversal. It uses recursive DFSUtil() 
        long[][] DFS(int v, long[] value) 
        { 
            // Mark all the vertices as not visited(set as 
            // false by default in java) 
            boolean[] visited = new boolean[V];
            long[] depth = new long[V];
            long[] sum = new long[V];
            ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();

            // Call the recursive helper function to print DFS traversal 
            // for (int i=0; i<V; i++)
            // {
            //     if (!visited[i])
            //         components.add(DFSUtil(i, visited, new ArrayList<Integer>(), value, 0, depth));
            // }
            long[][] ans = new long[visited.length][2];
            for(int i=0; i<visited.length; i++) {
                long[] jar = DFSUtil(v, visited, new ArrayList<Integer>(), value, depth, sum);
                ans[i][0] = jar[i];
            }
            ans[0][1] = costa;
            return ans;
        }
                // A function used by DFS 
        int[] DFSUtildp(int v, boolean[] visited, ArrayList<Integer> arrli, int[] value, int[] subtreedp, int[] sum) 
        { 
            // Mark the current node as visited and print it 
            visited[v] = true;
            arrli.add(v);
            
    
            // Recur for all the vertices adjacent to this vertex 
            Iterator<Integer> i = adj[v].listIterator(); 
            while (i.hasNext()) 
            { 
                int n = i.next(); 
                if (!visited[n]) {
                    DFSUtildp(n, visited, arrli, value, subtreedp, sum);
                    subtreedp[v] += (subtreedp[n]+ sum[n]);
                }
            }
            return subtreedp;
        } 
    
        // The function to do DFS traversal. It uses recursive DFSUtil() 
        int[] DFSdp(int v, int[] value, int[] sum) 
        { 
            // Mark all the vertices as not visited(set as 
            // false by default in java) 
            boolean[] visited = new boolean[V];
            int[] subtreedp = new int[V];
            ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();
    
            // // Call the recursive helper function to print DFS traversal 
            // for (int i=0; i<V; i++)
            // {
            //     if (!visited[i])
            //         components.add(DFSUtil(i, visited, new ArrayList<Integer>()));
            // }
            return DFSUtildp(v, visited, new ArrayList<Integer>(), value, subtreedp, sum);
        }


        // A function used by DFS 
        long[] DFSfinalUtil(int v, boolean[] visited, ArrayList<Integer> arrli, long[] sum, long[] cost) 
        { 
            // Mark the current node as visited and print it 
            visited[v] = true;
            arrli.add(v);
            
    
            // Recur for all the vertices adjacent to this vertex 
            Iterator<Integer> i = adj[v].listIterator(); 
            while (i.hasNext()) 
            { 
                int n = i.next();
                if (!visited[n]) {
                    cost[n] = cost[v] - 2*sum[n] + sum[0];
                    DFSfinalUtil(n, visited, arrli, sum, cost);
                }
            }
            return cost;
        } 
    
        // The function to do DFS traversal. It uses recursive DFSUtil() 
        long[] DFSfinal(int v, long[] sum, long[] cost) 
        { 
            // Mark all the vertices as not visited(set as 
            // false by default in java) 
            boolean[] visited = new boolean[V];
            ArrayList<ArrayList<Integer>> components = new ArrayList<ArrayList<Integer>>();
    
            // // Call the recursive helper function to print DFS traversal 
            // for (int i=0; i<V; i++)
            // {
            //     if (!visited[i])
            //         components.add(DFSUtil(i, visited, new ArrayList<Integer>()));
            // }

            return DFSfinalUtil(v,visited, new ArrayList<Integer>(),sum, cost);
        } 
    } 
}