#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

ll n;
vector<int> c;
vector<vector<int>> g;
vector<ll> ans;
vector<ll> siz;

ll sdfs(int pre,int now){
    ll ret=1;
    for(auto to:g[now])if(pre!=to) ret+=sdfs(now,to);
    return siz[now]=ret;
}


map<ll,ll> dfs(int pre,int now){
    vector<map<ll,ll>> v;
    for(auto to:g[now])if(pre!=to){
        auto mp=dfs(now,to);
        ll k=siz[to]-mp[c[now]];
        ans[c[now]]+=k*(k+1)/2;
        v.push_back(mp);
    }
    if(v.empty()){
        map<ll,ll> k;k[c[now]]++;
        return k;
    }
    sort(ALL(v),[](map<ll,ll> a,map<ll,ll> b){return a.size()>b.size();});
    for(int i=1;i<v.size();i++)for(auto &p:v[i]) v[0][p.first]+=p.second;
    v[0][c[now]]=siz[now];
    return v[0];
}


signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    cin>>n;
    c.resize(n);g.resize(n);ans.resize(n,0);siz.resize(n);
    rep(i,n){
        cin>>c[i];c[i]--;
    }
    rep(i,n-1){
        int u,v;cin>>u>>v;u--,v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    sdfs(-1,0);
    auto las=dfs(-1,0);
    for(auto &p:las){
        ll k=n-p.second;
        ans[p.first]+=k*(k+1)/2;
    }
    rep(i,n) cout<<(ans[i]?n*(n+1)/2-ans[i]:0)<<endl;
    return 0;
}
