#include <bits/stdc++.h>

#define int         long long
#define pb          push_back
#define pii         pair<int,int>
#define vi          vector<int>
#define vii         vector<pii>
#define mi          map<int,int>
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define x           first
#define y           second
#define sz(x)       (int)x.size()
#define endl        '\n'
#define hell        1000000007
#define rep(i,a,b)  for(int i=a;i<b;i++)
using namespace std;
int n,c[200005],ans[200005],ent[200005],s[200005],tim;
vi a[200005],f[200005];
void dfs(int node,int par){
    ent[node]=++tim;
    s[node]=1;
    for(auto i:a[node]){
        if(i!=par){
            dfs(i,node);
            int w=s[i];
            while(sz(f[c[node])) w-=s[f[c[node]].back()],f[c[node]].pop_back();
            s[node]+=s[i];
            ans[c[node]]+=w*(w+1)/2;
        }
    }
    f[c[node]].pb(node);
}
void solve(){
    cin>>n;
    rep(i,1,n+1) cin>>c[i];
    rep(i,0,n-1){
        int x,y;
        cin>>x>>y;
        a[x].pb(y);
        a[y].pb(x);
    }
    dfs(1,0);
    rep(i,1,n+1){
        int w=n;
        while(sz(f[i])) w-=s[f[i].back()],f[i].pop_back();
        ans[i]+=w*(w+1)/2;
        cout<<n*(n+1)/2-ans[i]<<endl;
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}