#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
vector<ll>g[N];
ll n,u,v,color[N],c[N],son[N],res[N]={0};
ll f(ll n) {return n*(n+1)/2;}
void dfs(ll u,ll fa){
    auto tmp=c[color[u]];
    for(auto v:g[u]){
        if(v==fa) continue;
        c[color[u]]=0;
        dfs(v,u);
        son[u]+=son[v];
        res[color[u]]+=f(son[v]-c[color[u]]);
    }
    c[color[u]]=tmp+son[u];
}

int main(){
    cin>>n;
    for(ll i=1;i<=n;i++){
        son[i]=1;
        cin>>color[i];
    }
    for(ll i=1;i<n;i++){
        cin>>u>>v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(1,-1);
    for(int i=1;i<=n;i++){
        cout<<f(n)-f(n-c[i])-res[i]<<endl;
    }
    return 0;
}
