#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9+7;
const long long MAXN = 5e3+5;
long long n;
vector<long long> v1[MAXN];
long long ans[MAXN];
long long res[MAXN];
long long col[MAXN];

long long sz[MAXN];
long long f(long long x){
    return 1LL*x*(x+1)/2;
}
void dfs(long long curr, long long par){
    long long tmp = res[col[curr]];
    for(long long x:v1[curr]){
        if(x!=par){
            res[col[curr]] = 0;
            dfs(x,curr);
            sz[curr] += sz[x];
            ans[col[curr]]-=f(sz[x]-res[col[curr]]);
        }
    }
    res[col[curr]] = tmp+sz[curr];
}
int main() {
    cin>>n;
    for(long long i=1;i<=n;i++){
        cin>>col[i];
        sz[i] = 1;
    }
    for(long long i=1;i<n;i++){
        long long x,y;
        cin>>x>>y;
        v1[x].push_back(y);
        v1[y].push_back(x);
    }
    dfs(1,1);
    for(long long i=1;i<=n;i++){
        //cout<<col[i]<<endl;
        cout<<(ans[i]+f(n)-f(n-res[i]))<<endl;
    }
}