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.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
	int[]a,next,head,to;
	int[]son,siz;
	int[]cla;
	long[]ans;
	int n,nn;
	void dfs(int u,int f) {
		siz[u]=1;
		int max=0;
		for(int j=head[u];j>0;j=next[j]) {
			int v=to[j];
			if(v==f)continue;
			dfs(v,u);
			if(siz[v]>max) {
				max=siz[v];
				son[u]=v;
			}
			siz[u]+=siz[v];
		}
	}
	int max=0;
	long sum;
	void add(int x,boolean book) {
		if(!book) {
			cla[x]--;
			return;
		}
		cla[x]++;
		if(cla[x]==max)sum+=x;
		else if(cla[x]>max) {
			max=cla[x];
			sum=x;
		}
	}
	void dfs2(int u,int f,boolean keep) {
		for(int j=head[u];j>0;j=next[j]) {
			int v=to[j];
			if(v==f||v==son[u])continue;
			dfs2(v,u,false);
		}
		max=0;
		if(son[u]!=0)dfs2(son[u],u,true);
//		System.out.println("u:"+u+" "+sum+" "+son[u]);
		for(int j=head[u];j>0;j=next[j]) {
			int v=to[j];
			if(v==f||v==son[u])continue;
			dfs3(v,u,true);
		}
		add(a[u],true);
//		show();
		ans[u]=sum;
		if(!keep) {
			for(int j=head[u];j>0;j=next[j]) {
				int v=to[j];
				if(v==f)continue;
				dfs3(v,u,false);
			}
			add(a[u],false);
		}
		
	}
	void dfs3(int u,int f,boolean book) {
		add(a[u],book);
		for(int j=head[u];j>0;j=next[j]) {
			int v=to[j];
			if(v==f)continue;
			dfs3(v,u,book);
		}
	}
	void show() {
		for(int i=1;i<10;i++) {
			System.out.print(i+" ");
		}
		System.out.println();
		for(int i=1;i<10;i++) {
			System.out.print(cla[i]+" ");
		}
		System.out.println();
		System.out.println();
	}
	int cnt;
	void add(int u,int v) {
		to[cnt]=v;
		next[cnt]=head[u];
		head[u]=cnt++;
	}
	void run(String[]args) throws IOException{
		Scanner in=new Scanner(System.in);
		n=in();
		nn=n+1;
		a=new int[nn];
		next=new int[n<<1];
		head=new int[n<<1];
		to=new int[n<<1];
		son=new int[nn];
		siz=new int[nn];
		ans=new long[nn];
		cla=new int[nn];
		for(int i=1;i<=n;i++)a[i]=in();
		cnt=1;
		for(int i=1;i<n;i++) {
			int u=in();
			int v=in();
			add(u,v);
			add(v,u);
		}
		dfs(1,0);
		dfs2(1,0,true);
		out.print(ans[1]);
		for(int i=2;i<=n;i++) {
			out.print(" "+ans[i]);
		}
		out.println();
		out.flush();
//		for(int u=1;u<=n;u++) {
//			System.out.print(u+":");
//			for(int j=head[u];j>0;j=next[j]) {
//				int v=to[j];
//				System.out.print(v+" ");
//			}
//			System.out.println();
//		}
		out.flush();
	}
	public static void main(String[]args) throws IOException {
//		Scanner in=new Scanner(System.in);
		new Main().run(args);
	}
	StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
	public double ind() throws IOException {
		in.nextToken();
		return in.nval;
	}
	public int in() throws IOException {
		in.nextToken();
		return(int)in.nval;
	}
	public long inl() throws IOException {
		in.nextToken();
		return(long)in.nval;
	}
	public String ins()throws IOException{
		in.nextToken();
		return in.sval;
	}
}