#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()

using namespace std;

const ll maxN = 200005;
ll n;
vector<ll> adj[maxN];
ll c[maxN];
ll sz[maxN];
ll done[maxN];
ll ans[maxN];

void dfs(ll u, ll p) {
	sz[u] = 1;
	ll done_before = done[c[u]];
	for(auto v : adj[u]) {
		if(v != p) {
			done[c[u]] = 0;
			dfs(v, u);
			sz[u] += sz[v];
			ll cnt = sz[v] - done[c[u]];
			ans[c[u]] += cnt*(cnt+1)/2;
			// cout<<u+1<<" -> "<<cnt<<" = "<<sz[v]<<" - "<<done[c[u]]<<endl;
		}
	}
	done[c[u]] = done_before + sz[u];
}

void solve(ll tc) {
	cin>>n;
	for(ll i=0; i<n; ++i) {
		cin>>c[i];
		--c[i];
	}
	for(ll i=0; i<n-1; ++i) {
		ll u, v;
		cin>>u>>v;
		adj[u-1].pb(v-1);
		adj[v-1].pb(u-1);
	}
	dfs(0, -1);
	for(ll i=0; i<n; ++i) {
		ll cnt = sz[0] - done[i];
		ans[i] += cnt*(cnt+1)/2;
		cout<<n*(n+1)/2 - ans[i]<<endl;
	}
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    // cin>>t;
    for(ll tc=1; tc<=t; ++tc) {
        solve(tc);
    }    
    return 0;
}