#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif

#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define rsz resize()
#define lb lower_bound
#define ub upper_bound
#define br cout << endl

typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); } 
int bit(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bit(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }

template<typename T>
void leftShift(vector<T> &v, ll k) { k %= sz(v); if(k < 0) k += sz(v); rotate(v.begin(), v.begin() + k, v.end()); }
template<typename T>
void rightSift(vector<T> &v, ll k) { leftShift(v, sz(v) - k); }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll rand(ll l, ll r){
	uniform_int_distribution<ll> uid(l, r);
	return uid(rng);
}

inline int nxt() {
	int x;
	cin >> x;
	return x;
}

inline int nxtll() {
	ll x;
	cin >> x;
	return x;
}

void pr() {}
void sc() {}

template <typename Head, typename... Tail>
void pr(Head H, Tail... T) { cout << H << " "; pr(T...); }

template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }

#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#ifndef LOCAL
string to_string(__int128 x) {
	string s = "";
	bool neg = 0;
	if(x < 0) { s += "-"; neg = 1; }
	if(!x) s += '0';
	while(x) {
		int rem = x % 10;
		s += to_string(rem);
		x /= 10;
	}
	reverse(s.begin() + neg, s.end());
	return s;
}
#endif

const int mod = 1e9 + 7; // 998244353;

int pwr(int a,int b) {
	int ans = 1;
	while(b) {
		if(b & 1) ans = (ans * 1LL * a) % mod;
		a = (a * 1LL * a) % mod;
		b >>= 1;
	}
	return ans;
}

/*
	Lookout for overflows!!
	Check array sizes!!
	Clear before test cases!!
	Use the correct modulo!!
	Check for corner cases!!
	Are you forgetting something?!
*/

#define int long long

const int N = 2e5 + 5;
const int LOGN = 19;
int c[N];
int sz[N];
vector<int> g[N];
int par[N];
vector<int> last[N];
int dp[LOGN][N];
int depth[N];
bool mark[N];
ll lol[N];

void dfs(int u,int p) {
	depth[u] = depth[p] + 1;
	dp[0][u] = p;
	sz[u] = 1;
	if(!last[c[u]].empty()) {
		par[u] = last[c[u]].back();
	}
	last[c[u]].pb(u);
	for(int v : g[u]) {
		if(v != p) {
			dfs(v, u);
			sz[u] += sz[v];
		}
	}
	last[c[u]].pop_back();
}

int m[N];

void solve() {
	int n = nxt();
	fr(i, 1, n) {
		c[i] = nxt();
	}
	fr(i, 1, n - 1) {
		int u = nxt(), v = nxt();
		g[u].pb(v);
		g[v].pb(u);
	}
	dfs(1, 0);
	fr(k, 1, LOGN - 1) {
		fr(i, 1, n) {
			dp[k][i] = dp[k - 1][dp[k - 1][i]];
		}
	}
	ll tot = n * 1LL * (n + 1) / 2;
	vector<int> ans(n + 1, tot);
	fr(i, 1, n) {
		lol[i] = n;
		m[i] = sz[i];
	}
	fr(i, 1, n) {
		int ii;
		if(par[i]) {
			int d1 = depth[par[i]], d2 = depth[i];
			int d = d2 - d1 - 1;
			ii = i;
			for(int k = LOGN - 1; k >= 0; k--) {
				if((d >> k) & 1) {
					ii = dp[k][ii];
				}
			}
		}
		else {
			lol[c[i]] -= sz[i];
			continue;
		}
		mark[ii] = 1;
		m[ii] -= sz[i];
	}
	fr(i, 2, n) {
		ans[c[dp[0][i]]] -= m[i] * 1LL * (m[i] + 1) / 2;
	}
	fr(i, 1, n) {
		ans[i] -= lol[i] * 1LL * (lol[i] + 1) / 2;
		pr(ans[i]);
		br;
	}
}

signed main() {
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t;
	// cin >> t;
	t = 1;
	for(int tt = 1; tt <= t; tt++) {
		solve();
	}
	return 0;
}

Measured memory_bytes: 79420