#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int,int>::iterator
#define sit set<pii>::iterator
#define itrm(g,x) for(mit g=x.begin();g!=x.end();g++)
#define itrs(g,x) for(sit g=x.begin();g!=x.end();g++)
#define ltype int
#define rep(i,j,k) for(ltype(i)=(j);(i)<=(k);(i)++)
#define rap(i,j,k) for(ltype(i)=(j);(i)<(k);(i)++)
#define per(i,j,k) for(ltype(i)=(j);(i)>=(k);(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
const int inf=0x3f3f3f3f,mod=1000000007;
const double pi=3.1415926535897932,eps=1e-6;
void chmax(int &x,int y){if(x < y) x = y;}
void chmin(int &x,int y){if(x > y) x = y;}
int n,c[200005],cnt,in[200005],out[200005];ll ans[200005];
vector<int> v[200005];
set<pii> s[200005];
void dfs1(int x,int fa){
    in[x] = ++cnt;
    rap(i,0,v[x].size()){
        int to = v[x][i];
        if(to == fa) continue;
        dfs1(to,x);
    }
    out[x] = cnt;
}
void dfs(int x,int fa){
    rap(i,0,v[x].size()){
        int to = v[x][i];
        if(to == fa) continue;
        dfs(to,x);
        int cur = out[to] - in[to] + 1;
        sit l = s[c[x]].lower_bound(mpr(in[to],0)), r = s[c[x]].lower_bound(mpr(out[to]+1,0));
        vector<pii> del;
        for(;l != r;l++) {
            cur -= l -> se;
            del.pb(*l);
        }
        ans[c[x]] -= (ll)cur * (cur + 1) / 2;
        while(del.size()){s[c[x]].erase(del.back());del.pop_back();}
    }
    s[c[x]].insert(mpr(in[x],out[x] - in[x] + 1));
}
int main()
{
    scanf("%d",&n);
    rep(i,1,n) scanf("%d",c+i);
    rap(i,1,n){
        int t1,t2;
        scanf("%d%d",&t1,&t2);
        v[t1].pb(t2);
        v[t2].pb(t1);
    }
    dfs1(1,0);
    rep(i,1,n) ans[i] = (ll)n * (n + 1) / 2;
    dfs(1,0);
    rep(i,1,n) if(c[1] != i){
        int cur = n;
        for(sit it = s[i].begin();it != s[i].end();it++) cur -= it -> se;
        ans[i] -= (ll)cur * (cur + 1) / 2;
    }
    rep(i,1,n) printf("%lld\n",ans[i]);
    return 0;
}