#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}

vector<vector<int>> v(200020);
ll n,c[200020];
vector<vector<ll>> cnt(200020);
ll sum[200020];
ll rem[200020];

int dfs(int p, int pre){
    int ret = 1;
    for(auto i:v[p]){
        if(i != pre){
            int t = dfs(i,p);
            cnt[p].push_back(t);
            ret += t;
        }
    }
    rem[p] = n - ret;
    return ret;
}

ll ans[200020];
ll used[200020];

void f(int p, int pre){
    // ans[c[p]] += rem[p] - used[c[p]];
    ll ret = 0;
    for(auto i:cnt[p]) ret += i;
    ans[c[p]] += ret * (rem[p] - used[c[p]]);
    ans[c[p]] += rem[p]+1 -used[c[p]] + ret;
    for(auto i:cnt[p]){
        ret -= i;
        ans[c[p]] += i*ret;
    }
    // used[c[p]] += rem[p] - used[c[p]] + 1;
    //cerr << "db " << p << " " << c[p] << " " << ans[c[p]] << endl;
    int k = 0;
    for(auto i:v[p]){
        if(i != pre){
            used[c[p]] = n - cnt[p][k];
            // sum[c[p]] = rem[c[p]] - used[c[p]];
            f(i,p);
            k++;
        }
    }

}


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    cin>>n;
    for(int i=0;i<n;i++){
        cin>>c[i];
        c[i]--;
    }
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--,b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    for(int i=0;i<n;i++){
        sum[i]=n;
    }
    dfs(0,-1);
    f(0,-1);
    for(int i=0;i<n;i++){
        cout << ans[i] << "\n";
    }
}