#include <cstring>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <map>
#include<time.h>
using namespace std;
typedef long long ll;
const int maxn=2e5+10;
struct edge {
	int to,Next;
}Edge[maxn<<1];
int Head[maxn],tot;
ll val[maxn],sum[maxn],sz[maxn],rev[maxn];
void Add(int from, int to) {
	Edge[tot]={to,Head[from]};
	Head[from]=tot++;
	Edge[tot]={from,Head[to]};
	Head[to]=tot++;
}
void dfs(int u, int fa) {
	sz[u]=1;
	ll num=sum[val[u]];
	ll Last=sum[val[u]];
	for (int i=Head[u]; ~i; i=Edge[i].Next) {
		int v=Edge[i].to;
		if (v==fa) continue;
		dfs(v,u);
		sz[u]+=sz[v];
		ll cnt=sz[v]-(sum[val[u]]-Last);
		rev[val[u]]+=(1+cnt)*cnt/2;
		Last=sum[val[u]];
	}
	sum[val[u]]+=sz[u]-(sum[val[u]]-num);
} 
int main(){
	memset(Head,-1,sizeof(Head));
    int n;
    scanf("%d",&n);
    for (int i=1; i<=n; i++) {
    	scanf("%lld",&val[i]);
	}
    for (int i=1; i<n; i++) {
    	int u,v;
    	scanf("%d%d",&u,&v);
    	Add(u,v);
	}
    dfs(1,0);
    ll Max=1ll*n*(n+1)/2;
    for (int i=1; i<=n; i++) {
    	if (sum[i]<n) {
    		ll cnt=n-sum[i];
    		rev[i]+=(cnt+1)*cnt/2;
		}
	}
	for (int i=1; i<=n; i++) printf("%lld\n",Max-rev[i]);
    return 0;
}
