#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 = 998244353;

ll n, a[N];
vec G[N];
int sz[N];
ll ans[N];
int cnt[N];
bool vis[N];
int big[N];

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

void GetSz(int u, int p)
{
	sz[u] = 1;
	big[u] = -1;
	for (auto v : G[u])
		if (v != p)
		{
			GetSz(v, u);
			sz[u] += sz[v];
			if (big[u] == -1 || sz[big[u]] < sz[v])
				big[u] = v;
		}
}

void add(int u, int p, int val)
{
	if (val == 1)
	{
		if (vis[a[u]] == 0)
			cnt[a[u]] += sz[u];
	}
	else
	{
		if (vis[a[u]] == 0)
			cnt[a[u]] -= sz[u];
	}
	vis[a[u]] = true;
	for (auto v : G[u])
		if (v != p)
			add(v, u, val);
	vis[a[u]] = false;
}

void DFS(int u, int p, bool keep)
{
	for (auto v : G[u])
		if (v != p && v != big[u])
			DFS(v, u, 0);
	if (big[u] != -1)
		DFS(big[u], u, 1);
	for (auto v : G[u])
		if (v != p && v != big[u])
			add(v, u, 1);
	int c = a[u];
	ans[c] += f(sz[u] - 1 - cnt[c]);
	if (keep == 0)
	{
		for (auto v : G[u])
			if (v != p)
				add(v, u, -1);
	}
	else cnt[a[u]] = sz[u];
}


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);
	}
	GetSz(1, 1);
	DFS(1, 1, 1);
	for (int i = 1; i <= n; i++)
		if (i != a[1])
			ans[i] += f(sz[1] - cnt[i]);
	for (int i = 1; i <= n; i++)
		cout << n * (n + 1) / 2 - ans[i] << ' ';
	return 0;
}