#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
typedef long long   ll;
#define pb                push_back
#define mp                make_pair
#define all(a)            (a).begin(), (a).end()
#define clr(a,h)          memset(a, (h), sizeof(a))
#define mem(a,h)          memset(a, (h), sizeof(a))
#define fi first
#define se second
#define por(a,b) (((a%MOD) * (b%MOD))%MOD)
#define forg(i, b, e, c)    for (ll i = (ll)b; i < (ll)e; i+=c)
#define forr(i, b, e)    for (ll i = b; i < e; i++)
 
 
using namespace std;
//using namespace __gnu_pbds;
typedef double lldb;
typedef pair<ll, ll>  ii;
typedef pair<ll, ii>  iii;
typedef vector<ll>     vi;
typedef vector<vi>     vvi;
typedef vector<ii>      vii;
typedef vector<ll>      vll;
//typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
const ll INF = 1e9;
const double PI = acos(-1);
#define tam 220000
#define offset 
const ll MOD= 1e9+7;



#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {

template < class c > struct rge { c b, e; };
template < class c > rge<c> range(c i, c j) { return rge<c>{i, j}; }
template < class c > auto dud(c* x) -> decltype(cerr << *x, 0);
template < class c > char dud(...);

struct debug {
    ~debug() { cerr << endl; }
    
    //     Part 1
    
    template < class c > typename \
    enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) {
        cerr << boolalpha << i;
        return * this;
    }
    
    //     Part 2
    
    template < class c > typename \
    enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) {
        return * this << range(begin(i), end(i)); 
    }
    
    //   Part 3
    
    template < class c, class b > debug & operator << (pair < b, c > d) {
        return * this << "(" << d.first << ", " << d.second << ")";
    }
    
    //   Part 4
    
    template < class c > debug & operator <<(rge<c> d) {
        *this << "[";
        for (auto it = d.b; it != d.e; ++it)
            *this << ", " + 2 * (it == d.b) << *it;
        return * this << "]";
    }
    
};
#define qwer(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

ll vcnt[tam],color[tam];
vector<int> g[tam];
void dfs0(int u,int pa)
{
    vcnt[u]=1;
    for(int v:g[u])
    {
        if (v==pa)continue;
        dfs0(v,u);
        vcnt[u]+=vcnt[v];
    }
}
vector<int> padres[tam];
ll hijos[tam],total[tam];
ll formas[tam],restar[tam];
void dfs1(int u,int pa)
{
    int mipa=-1;    
    padres[color[u]].pb(u);
    vector<int> misrestas;
    ll totsons=0;
    for(int v:g[u])
    {
        if (v==pa)continue;
        restar[u]=0;
        dfs1(v,u);

        totsons+=vcnt[v]-restar[u];
        misrestas.pb(vcnt[v]-restar[u]);
    }
    int micolor=color[u];

    for(ll numrest:misrestas)
    {
        formas[micolor]+=numrest*(totsons-numrest);
        totsons-=numrest;
    }

    padres[color[u]].pop_back();
    
    formas[micolor]+=hijos[u]+ hijos[u]*(total[micolor]-hijos[u]);
    total[micolor]-=hijos[u];


    if (!padres[micolor].empty()){
        mipa=padres[micolor].back();
        hijos[mipa]-=vcnt[u];

        restar[mipa]+=vcnt[u];
    }

}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);
    int n;
    cin>>n;
    forr(i,1,n+1)
    {
        cin>>color[i];
    }    
    int iz,der;
    forr(i,0,n-1)
    {
        cin>>iz>>der;
        g[iz].pb(der);
        g[der].pb(iz);
    }
    dfs0(1,1);
    forr(i,0,n+1){
        hijos[i]=vcnt[i];
        total[i]=n;
    }
    dfs1(1,1);
    forr(i,1,n+1)
    cout<<formas[i]<<endl;
}
// PLUS ULTRA!


Measured memory_bytes: 61604