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.util.*;
public final class dsu_on_tree
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static ArrayList<Integer>[] al1,al2;
	static int maxn=(int)(1e5),n;
	static int[] tree,size,a,cnt;
	static long[] sums,res;
	
	static void update(int node,int s,int e,int l,int r,int val)
	{
		if(s>e || l>e || r<s)
		{
			return;
		}
		if(s==e)
		{
			tree[node]+=val;
		}
		else
		{
			int mid=(s+e)>>1;
			update(node<<1,s,mid,l,r,val);update(node<<1|1,mid+1,e,l,r,val);
			tree[node]=Math.max(tree[node<<1],tree[node<<1|1]);
		}
	}
	
	static void dfs(int u,int p,boolean keep)
	{
		int max=-1,big_child=-1;
		for(int x:al1[u])
		{
			if(x!=p && size[x]>max)
			{
				max=size[x];big_child=x;
			}
		}
		for(int x:al1[u])
		{
			if(x!=p && x!=big_child)
			{
				dfs(x,u,false);
			}
		}
		if(big_child!=-1)
		{
			dfs(big_child,u,true);al2[u]=al2[big_child];
		}
		al2[u].add(u);update(1,1,n,a[u],a[u],1);
		int temp=cnt[a[u]];sums[temp]-=a[u];cnt[a[u]]++;sums[cnt[a[u]]]+=a[u];
		for(int x:al1[u])
		{
			if(x!=p && x!=big_child)
			{
				for(int y:al2[x])
				{
					al2[u].add(y);update(1,1,n,a[y],a[y],1);
					temp=cnt[a[y]];sums[temp]-=a[y];cnt[a[y]]++;sums[cnt[a[y]]]+=a[y];
				}
			}
		}
		res[u]=sums[tree[1]];
		if(!keep)
		{
			for(int x:al2[u])
			{
				update(1,1,n,a[x],a[x],-1);temp=cnt[a[x]];sums[temp]-=a[x];cnt[a[x]]--;sums[cnt[a[x]]]+=a[x];
			}
		}
	}
	
	static void build(int u,int p)
	{
		for(int x:al1[u])
		{
			if(x!=p)
			{
				build(x,u);size[u]+=size[x];
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String args[]) throws Exception
	{
		n=sc.nextInt();a=new int[n+1];tree=new int[4*(n+1)];res=new long[n+1];size=new int[n+1];sums=new long[n+1];cnt=new int[n+1];al1=new ArrayList[n+1];al2=new ArrayList[n+1];boolean[] v=new boolean[n+1];
		for(int i=1;i<=n;i++)
		{
			a[i]=sc.nextInt();al1[i]=new ArrayList<Integer>();al2[i]=new ArrayList<Integer>();
			if(!v[a[i]])
			{
				sums[0]+=a[i];v[a[i]]=true;
			}
		}
		for(int i=1;i<n;i++)
		{
			int u1=sc.nextInt(),v1=sc.nextInt();al1[u1].add(v1);al1[v1].add(u1);
		}
		Arrays.fill(size,1);build(1,-1);dfs(1,-1,false);
		for(int i=1;i<=n;i++)
		{
			out.print(res[i]+" ");
		}
		out.println("");out.close();
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}