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

import javax.swing.plaf.basic.BasicScrollPaneUI.HSBChangeListener;

import java.io.*;

public class Absent_Students {
	static BufferedReader br;
	static PrintWriter pw;
	static int inf = (int) 1e9;
	static long mod = (long) 1e9 + 7;

	static ArrayList<Integer>[] g;
	static int n;
	static int[] a, length;
	static long[] sumA;

	static boolean[][] vis;

	public static void main(String[] args) throws NumberFormatException, IOException, InterruptedException {
//		br = new BufferedReader(new FileReader(new File("chief.in")));
//		br = new BufferedReader(new InputStreamReader(System.in));
		Scanner sc = new Scanner(System.in);
		pw = new PrintWriter(System.out);
		n = sc.nextInt();
		a = new int[n];
		length = new int[n];
		for (int i = 0; i < a.length; i++) {
			a[i] = sc.nextInt();
		}
		g = new ArrayList[n];
		for (int i = 0; i < g.length; i++) {
			g[i] = new ArrayList<>();
		}
		for (int i = 0; i < n - 1; i++) {
			int u = sc.nextInt() - 1;
			int v = sc.nextInt() - 1;
			g[u].add(v);
			g[v].add(u);
		}
		sumA = new long[n];
		dfs(0, -1, 0);
		long res = 0;
		for (int i = 0; i < a.length; i++) {
			res += a[i] * 1l * length[i];
		}
//		System.out.println(Arrays.toString(sumA));
//		System.out.println(Arrays.toString(length));
		best = res;
		find(0, -1, res);
		pw.println(best);
		pw.close();

	}

	static long best;

	static void find(int u, int pa, long res) {
		for (int x : g[u])
			if (x != pa) {
				long temp = res - sumA[x];
				temp += sumA[0] - sumA[x];
				best = Math.max(best, temp);
				find(x, u, temp);
			}
	}

	static long dfs(int u, int pa, int l) {
		length[u] = l;
		sumA[u] = a[u];
		for (int x : g[u]) {
			if (x != pa) {
				sumA[u] += dfs(x, u, l + 1);
			}
		}
		return sumA[u];
	}

//	static int diameter() {
//		int far = bfs(0);
//		int far2 = bfs(far);
//		return dist(far, -1, far2);
//	}
//
//	static int bfs(int u) {
//		Queue<Integer> q = new LinkedList<>();
//		q.add(u);
//		boolean[] vis = new boolean[n];
//		int far = -1;
//		while (!q.isEmpty()) {
//			int size = q.size();
//			for (int i = 0; i < size; i++) {
//				far = q.poll();
//				vis[far] = true;
//				for (int v : g[far]) {
//					if (!vis[v])
//						q.add(v);
//				}
//
//			}
//		}
//		return far;
//	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

		public Scanner(InputStream s) {
			br = new BufferedReader(new InputStreamReader(s));
		}

		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

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

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

		public String nextLine() throws IOException {
			return br.readLine();
		}

		public boolean ready() throws IOException {
			return br.ready();
		}

	}

	static int m, k;

	static double[] memo;
	static ArrayList<Integer>[] adj;

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

	// method to return LCM of two numbers
	static long lcm(long a, long b) {
		return (a * b) / gcd(a, b);
	}

	static double dis(int x, int y, int z, int w) {
		return Math.sqrt((x - z) * (x - z) + (y - w) * (y - w));
	}

	static int[] nxtarr() throws IOException {
		StringTokenizer st = new StringTokenizer(br.readLine());
		int[] a = new int[st.countTokens()];
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
		}
		return a;
	}

	static long[] nxtarrLong() throws IOException {
		StringTokenizer st = new StringTokenizer(br.readLine());
		long[] a = new long[st.countTokens()];
		for (int i = 0; i < a.length; i++) {
			a[i] = Long.parseLong(st.nextToken());
		}
		return a;
	}

	static class pair implements Comparable<pair> {
		long x;
		long y;

		public pair(long d, long u) {
			x = d;
			y = u;
		}

		@Override
		public int compareTo(pair o) {
			// TODO Auto-generated method stub
			if (y == o.y)
				return (int) (x - o.x);
			return (int) (y - o.y);
		}

	}

	static class triple implements Comparable<triple> {
		int x;
		int y;
		int z;

		public triple(int a, int b, int c) {
			x = a;
			y = b;
			z = c;
		}

		@Override
		public int compareTo(triple o) {
			// TODO Auto-generated method stub
			return x - o.x;
		}
	}

}