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 cities {
	static int n;
	static int[] c, ti;
	static long[] ans;
	static HashMap<Long, Integer>[] h;
	static ArrayList<Integer>[] ad;

	public static void main(String[] args) throws IOException, InterruptedException {
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		n = sc.nextInt();
		c = new int[n];
		ti = new int[n];
		h = new HashMap[n];
		ad = new ArrayList[n];

		ans = new long[n];
		for (int i = 0; i < n; i++) {
			c[i] = sc.nextInt();
			h[i] = new HashMap<>();
			ad[i] = new ArrayList<>();

		}
		for (int i = 0; i < n - 1; i++) {
			int a = sc.nextInt() - 1;
			int b = sc.nextInt() - 1;
			ad[a].add(b);
			ad[b].add(a);
		}
		dfs(0, -1);
		for (int i = 0; i < n; i++)
			out.print(ans[i] + " ");
		out.flush();
	}

	static void dfs(int u, int p) {
		if (ad[u].size() == 1 && p != -1) {
			ans[u] = c[u];
			h[u].put(ans[u], 1);
			ti[u] = 1;
			return;
		}
		h[u].put((long) c[u], 1);
		int temp = 1;
		long ans1 = c[u];
		for (int j : ad[u])
			if (j != p)
				dfs(j, u);
		for (int v : ad[u]) {
			if (v != p) {
				if (h[v].size() > h[u].size()) {
				//	if (u == 0)
					//	System.out.println("OO" + h[v] + " " + h[u]);
					long ans2 = ans[v];
					int uu = ti[v];
					for (long k : h[u].keySet()) {
						int times = h[u].get(k) + h[v].getOrDefault(k, 0);
						h[v].put(k, times);
						if (times > uu) {
							ans2 = k;
							uu = times;
						} else if (times == uu) {
							ans2 += k;
						}
					}
					ans[u] = ans2;
					ti[u] = uu;
                    ans1=ans2;
                    temp=uu;
				//	System.out.println(ans[u]);
					h[u] = h[v];
				} else {
					for (long k : h[v].keySet()) {
						int times = h[v].get(k) + h[u].getOrDefault(k, 0);
						h[u].put(k, times);
						if (times > temp) {
							ans1 = k;
							temp = times;
						} else if (times == temp) {
							ans1 += k;
						}
					}
					ans[u] = ans1;
					ti[u] = temp;

				}
			}
		}

	}

	static class edge {
		char a;
		char b;

		edge(char a, char b) {
			this.a = a;
			this.b = b;
		}

		public String toString() {
			return a + " " + b;
		}
	}

	static class unionfind {
		int[] p;
		int[] size;
		Queue<Integer>[] ans;

		unionfind(int n) {
			p = new int[n];
			size = new int[n];
			ans = new LinkedList[n];
			for (int i = 0; i < n; i++)
				ans[i] = new LinkedList<>();
			for (int i = 0; i < n; i++) {
				p[i] = i;
				ans[i].add(i);
			}
			Arrays.fill(size, 1);
		}

		int findSet(int v) {
			if (v == p[v])
				return v;
			return p[v] = findSet(p[v]);
		}

		Queue<Integer> get() {
			for (int i = 0; i < ans.length; i++)
				if (ans[i].size() == ans.length)
					return ans[i];
			return null;
		}

		boolean sameSet(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return true;
			return false;
		}

		int max() {
			int max = 0;
			for (int i = 0; i < size.length; i++)
				if (size[i] > max)
					max = size[i];
			return max;
		}

		void combine(int a, int b) {
			a = findSet(a);
			b = findSet(b);
			if (a == b)
				return;
			if (size[a] > size[b]) {
				p[b] = a;
				size[a] += size[b];
				for (int k : ans[b])
					ans[a].add(k);

			} else {
				p[a] = b;
				size[b] += size[a];
				for (int k : ans[a])
					ans[b].add(k);
			}
		}
	}

	static class Scanner {
		StringTokenizer st;
		BufferedReader br;

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

		public Scanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}

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

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

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

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

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

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

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

		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
}
