#include <bits/stdc++.h>
using namespace std;
 
 
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a)  memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

void pre(){


}
 
void solve(){
 
 
}

vector<ll> c,g[200009],s,s2,dp;
map<ll,ll> dfs(int u, int p){
    map<ll, ll> m;
    vector<ll> w;
    s[u]=1;
    trav(v,g[u])if(v!=p){
        map<ll, ll> m2=dfs(v,u);
        s[u]+=s[v];
        if(m2.find(c[u])!=m2.end()) w.pb(s[v]-m2[c[u]]);
        else w.pb(s[v]);
        if(sz(m2)>sz(m))swap(m2,m);
        trav(it,m2)m[it.fst]+=it.snd;
    }
    //debug(u, p);
    //trav(it,m)cout<<"("<<it.fst<<", "<<it.snd<<"), ";
    //cout<<endl;
    //debug(w);
    ll su=0;
    rep(i,sz(w))su+=w[i];
    rep(i,sz(w))su-=w[i],dp[c[u]]+=su*1ll*w[i];
    s2[u]=s[u]-m[c[u]];
    m[c[u]]=s[u];
    return m;
}
int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	pre();

    int n;cin>>n;
    c.resize(n,0);
    s.resize(n,0);
    s2.resize(n,0);
    dp.resize(n,0);
    rep(i,n)cin>>c[i],c[i]--;
    rep(i,n-1){
        int u,v;cin>>u>>v;
        u--,v--;
        g[u].pb(v),g[v].pb(u);
    }
    rep(i,n)if(sz(g[i])<=1){dfs(i,-1);break;}
    rep(i,n)dp[c[i]] += s2[i]*1ll*(n-s[i]+1);
    rep(i,n)cout<<dp[i]<<'\n';

	return 0;
}
 

