You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour.

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c. So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of vertices in the tree.

The second line contains n integers ci (1 ≤ ci ≤ n), ci — the colour of the i-th vertex.

Each of the next n - 1 lines contains two integers xj, yj (1 ≤ xj, yj ≤ n) — the edge of the tree. The first vertex is the root of the tree.

Output

Print n integers — the sums of dominating colours for each vertex.

Examples

Input

4
1 2 3 4
1 2
2 3
2 4


Output

10 9 3 4


Input

15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13


Output

6 5 4 3 2 3 3 1 1 3 2 2 1 2 3
import java.util.*;
import java.io.*;

public class E600 
{
	public static void main(String[] args) { new E600(); }
	FS in = new FS();
	PrintWriter out = new PrintWriter(System.out);
	
	int n;
	int[] color, maxOcc;
	long[] domSum;
	ArrayList<Integer>[] adj;
	HashMap<Integer, Integer>[] colFrq;
	
	E600() {
		n = in.nextInt();
		color = new int[n];
		for (int i = 0; i < n; i++)
			color[i] = in.nextInt();
		adj = new ArrayList[n];
		for (int i = 0; i < n; i++)
			adj[i] = new ArrayList<>();
		for (int i = 0; i < n - 1; i++) {
			int u = in.nextInt() - 1;
			int v = in.nextInt() - 1;
			adj[u].add(v); adj[v].add(u);
		}

		maxOcc = new int[n];
		domSum = new long[n];
		colFrq = new HashMap[n];
		for (int i = 0; i < n; i++)
			colFrq[i] = new HashMap<>();
		dfs(0, 0);

		for (long i : domSum)
			out.print(i + " ");
		out.println();
		out.close();
	}

	void dfs(int node, int par) {
		int big = -1, bigSz = -1;
		for (int child : adj[node])
			if (child != par) {
				dfs(child, node);
				if (colFrq[child].size() > bigSz) {
					bigSz = colFrq[child].size();
					big = child;
				}
			}


		if (big != -1) {
			maxOcc[node] = maxOcc[big];
			domSum[node] = domSum[big];
			colFrq[node] = colFrq[big];
			for (int child : adj[node]) 
				if (child != par && child != big) {
					for (int col : colFrq[child].keySet()) {
						if (!colFrq[node].containsKey(col))
							colFrq[node].put(col, 0);
				
						int cur = colFrq[node].get(col);
						int add = colFrq[child].get(col);
						colFrq[node].put(col, cur + add);
						
						if (cur + add > maxOcc[node]) {
							maxOcc[node] = cur + add;
							domSum[node] = col;
						}
						else if (cur + add == maxOcc[node])
							domSum[node] += col;
					}
				}
		}
		

		if (!colFrq[node].containsKey(color[node]))
			colFrq[node].put(color[node], 0);

		int cur = colFrq[node].get(color[node]);
		colFrq[node].put(color[node], cur + 1);

		if (cur + 1 > maxOcc[node]) {
			maxOcc[node] = cur + 1;
			domSum[node] = color[node];
		}
		else if (cur + 1 == maxOcc[node])
			domSum[node] += color[node];
	}

	int min(int a, int b) { if (a < b) return a; return b; }	
	int max(int a, int b) { if (a > b) return a; return b; }	
	long min(long a, long b) { if (a < b) return a; return b; }	
	long max(long a, long b) { if (a > b) return a; return b; }	

	boolean z(int x) { if (x == 0) return true; return false; }
	boolean z(long x) { if (x == 0) return true; return false; }
	
	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch (Exception e) {}
			} return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next()); }
		long nextLong() { return Long.parseLong(next()); }
		double nextDouble() { return Double.parseDouble(next()); }
	}
}
