#include<bits/stdc++.h>
using namespace std;

#define int long long

const int N = 4e5 + 5;
typedef long long ll;

int n, first[N], c[N], cnt, top[N], f[N];
ll ans[N], siz[N], d[N];
struct Edge {
	int to, next;
} e[N<<1];

void add(int u, int v) {
	e[cnt] = (Edge) {v, first[u]};
	first[u] = cnt++;
}

void Dfs1(int u) {
	if(top[c[u]] == 0) {
		ans[c[u]] += n*(n - 1ll)/2 + 1ll;
		for(int i = first[u]; i != -1; i = e[i].next) {
			int v = e[i].to;
			if(v == f[u]) continue;
			ans[c[u]] -= siz[v]*(siz[v] - 1)/2;
		}
		ans[c[u]] -= (n - siz[u])*(n - siz[u] - 1)/2;
	}
	else {
		ans[c[u]] += (d[u] - d[top[c[u]]] - 1ll)*siz[u] + (siz[u] - 1)*siz[u]/2 + 1ll;
		for(int i = first[u]; i != -1; i = e[i].next) {
			int v = e[i].to;
			if(v == f[u]) continue;
			ans[c[u]] -= siz[v]*(siz[v] - 1ll)/2;
		}
	}
	for(int i = first[u]; i != -1; i = e[i].next) {
		int v = e[i].to;
		if(v == f[u]) continue;
		top[c[u]] = u;
		Dfs1(v);
	}
}

void Dfs(int u, int fa, int dep) {
	siz[u] = 1;
	f[u] = fa;
	d[u] = dep;
	for(int i = first[u]; i != -1; i = e[i].next) {
		int v = e[i].to;
		if(v == fa) continue;
		Dfs(v, u, d[u] + 1);
		siz[u] += siz[v];
	}
}

signed main() {
	cin>>n;
	memset(first, -1, sizeof(first));
	for(int i = 1; i <= n; i++) cin>>c[i];
	for(int i = 1; i < n; i++) {
		int u, v;
		cin>>u>>v;
		add(u, v);
		add(v, u);
	}
	Dfs(1, -1, 1);
	Dfs1(1);
	for(int i = 1; i <= n; i++)
		cout<<ans[i]<<endl;
	return 0;
}