#include<bits/stdc++.h>
#define GO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
const int N = 5e5 + 5;
const int M = 1e6 + 5;
const int Mod = 1e9 + 7;

int n, a[N];
vec G[N];
ll sum[N];
int sz[N];
ll ans[N];

ll f(ll x)
{
	return x * (x + 1) / 2;
}

void GetSz(int u, int p)
{
	sz[u] = 1;
	for (auto v : G[u])
	{
		if (v == p)continue;
		GetSz(v, u);
		sz[u] += sz[v];
	}
}

int Last[N];

void DFS(int u, int p)
{
	int color = a[u];
	int par = Last[color];
	sum[par] += sz[u];
	Last[color] = u;
	for (auto v : G[u])
	{
		if (v == p)continue;
		DFS(v, u);
		ans[color] += f(sz[v] - sum[u]);
		sum[u] = 0;
	}
	Last[color] = par;
}

int main()
{
	GO;
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> a[i];
	for(int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for (int i = 1; i <= n; i++)
		Last[i] = n + i;
	GetSz(1, 1);
	DFS(1, 1);
	for (int i = 1; i <= n; i++)
	{
		if (i == a[1])
			continue;
		ans[i] += f(n - sum[i + n]);
	}
	for (int i = 1; i <= n; i++)
		cout << (f(n) - ans[i] )  << '\n';
	return 0;
}