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.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.TreeMap;


public class D {

	static ArrayList<Integer>[] adjList;
	static int[] color, size;
	
	static int findSize(int u, int p)
	{
		int sz = 1;
		for(int v: adjList[u])
			if(v != p)
				sz += findSize(v, u);
		return size[u] = sz;
	}
	
	static FrequencyMap fm = new FrequencyMap();
	static int[] cnt;
	static long[] ans;
	
	static void add(int u, int p, int x)
	{
		int c = color[u];
		fm.erase(c, cnt[c]);
		fm.add(c, ++cnt[c]);
		for(int v: adjList[u])
			if(v != p && v != x)
				add(v, u, x);
	}
	
	static void erase(int u, int p)
	{
		int c = color[u];
		fm.erase(c, cnt[c]);
		fm.add(c, --cnt[c]);
		for(int v: adjList[u])
			if(v != p)
				erase(v, u);
	}
	
	static void dfs(int u, int p, boolean keep)
	{
		int heavy = -1, max = -1;
		for(int v: adjList[u])
			if(v != p && size[v] > max)
				max = size[heavy = v];
		for(int v: adjList[u])
			if(v != p && v != heavy)
				dfs(v, u, false);
		if(heavy != -1)
			dfs(heavy, u, true);
		
		add(u, p, heavy);
		ans[u] = fm.query();
		if(!keep)
			erase(u, p);
	}
	
	public static void main(String[] args) throws IOException 
	{
		Scanner sc = new Scanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int V = sc.nextInt();
		color = new int[V];
		for(int i = 0; i < V; ++i)
			color[i] = sc.nextInt();
		adjList = new ArrayList[V];
		for(int i = 0; i < V; ++i)
			adjList[i] = new ArrayList<Integer>();
		for(int i = 1; i < V; ++i)
		{
			int u = sc.nextInt() - 1, v = sc.nextInt() - 1;
			adjList[u].add(v);
			adjList[v].add(u);
		}
		
		size = new int[V];
		findSize(0, -1);
		
		ans = new long[V];
		cnt = new int[V + 1];
		for(int i = 1; i <= V; ++i)
			fm.add(i, 0);
		dfs(0, -1, true);
		
		StringBuilder sb = new StringBuilder();
		for(int i = 0; i < V; ++i)
			sb.append(ans[i] + " ");
		out.println(sb);
		out.flush();
		out.close();
	}
	
	static class FrequencyMap
	{
		TreeMap<Integer, Long> map = new TreeMap<Integer, Long>();
		
		void add(int color, int freq)
		{
			Long s = map.get(freq);
			if(s == null)
				s = 0l;
			map.put(freq, s + color);
		}
		
		void erase(int color, int freq)
		{
			Long s = map.get(freq);
			if(s == color)
				map.remove(freq);
			else
				map.put(freq, s - color);
		}
		
		long query() { return map.lastEntry().getValue(); }
	}

	static class Scanner 
	{
		StringTokenizer st;
		BufferedReader br;

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

		public Scanner(FileReader r){	br = new BufferedReader(r);}

		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 double nextDouble() throws IOException { return Double.parseDouble(next()); }

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


	}
} 