#include <bits/stdc++.h>
#define LL long long
#define maxn 200010
using namespace std;
int color[maxn],head[maxn],tot,n,Size[maxn],sum[maxn];
LL ans[maxn];
struct node{
	int to,next;
}e[maxn<<1];
void add_edge(int u,int v){
	tot++,e[tot].to=v,e[tot].next=head[u],head[u]=tot;
}
LL get_num(LL x){
	return x*(x+1)/2;
}
void dfs(int u,int fa){
	int v,b,c,t,dt;
	LL pre;
	Size[u]=1,c=color[u], pre=sum[c];//非u子树已算出的c颜色为根的节点数量
	for(b=head[u];b;b=e[b].next){
		v=e[b].to;
		if(v==fa)continue;
		t=sum[c];
		dfs(v,u);
		dt=sum[c]-t;//遍历了v之后，c颜色对应的变化量
		Size[u]+=Size[v];
		ans[c]-=get_num(Size[v]-dt);
	}

	sum[c]=pre+Size[u];//总的c颜色为根的节点数量
	//cout << "u: " << u << "    pre: " << pre << "     c: " << c << "    sum: " << sum[c] << "     Size: " << Size[u] << "    ans: " << ans[c] << endl;
}
int main(){
	int i,u,v;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&color[i]);
	for(i=1;i<n;i++){
		scanf("%d%d",&u,&v);
		add_edge(u,v),add_edge(v,u);
	}
	for(i=1;i<=n;i++)ans[i]=get_num(n);
	dfs(1,0);
	for(i=1;i<=n;i++)ans[i]-=get_num(n-sum[i]);//n-sum[i]指离树根最近的i颜色之外的其他颜色(整棵树扣除所有以i颜色为根的子树)数量
	for(i=1;i<=n;i++)printf("%lld\n",ans[i]);
	return 0;
}

/*
7
1 2 4 1 2 3 1
1 2
1 3
2 4
2 5
3 6
3 7

10
1 2 4 1 2 3 1 5 6 7
1 2
1 3
2 4
2 5
3 6
3 7
4 8
7 9
7 10
*/
