#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long 
#define FI first 
#define SE second
#define pb push_back 
#define eb emplace_back
#define mod 1000000007
#define all(c) (c).begin(),(c).end()
#define LB  lower_bound
#define UB  upper_bound
#define max3(a,b,c) max(c,max(a,b))
#define min3(a,b,c) min(c,min(a,b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define OOK order_of_key  //no of elements less than
#define FBO find_by_order //iterator pointing kth element;indexing starts from 0
#define CK3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define CK4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl

typedef pair<int,int> PII;
typedef pair<pair<int,int>,int> PPII;
typedef pair<int,pair<int,int>> PIPI;
typedef map<int,int>MII;
typedef vector<int> VI;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*int power(int a, int b)
{int x=1,y=a;while(b>0){if(b%2){x =(x*y)%mod;}y =(y*y)%mod;b/=2;}return x%mod;}
*/
//int modInverse(int a, int m) {return power(a, m-2);} //gcd(a,m)=1 

int t[1000000];vector<VI>adj;VI ans(1000000,0);VI colour(1000000,0);int n;
void update(int v, int tl, int tr, int l, int r, int add) {
    if (l > r)
        return;
    if (l == tl && r == tr) {
        t[v] += add;
    } else {
        int tm = (tl + tr) / 2;
        update(v*2, tl, tm, l, min(r, tm), add);
        update(v*2+1, tm+1, tr, max(l, tm+1), r, add);
    }
}

int get(int v, int tl, int tr, int pos) {
    if (tl == tr)
        return t[v];
    int tm = (tl + tr) / 2;
    if (pos <= tm)
        return t[v] + get(v*2, tl, tm, pos);
    else
        return t[v] + get(v*2+1, tm+1, tr, pos);
}
void dfs(int v,int par)
{   int px=get(1,1,n,colour[v]);update(1,1,n,colour[v],colour[v],-px);
    for(auto it:adj[v])
    {
        if(it!=par)
        {
            dfs(it,v);
            int x=get(1,1,n,colour[v]);
            if(x>0)
            ans[colour[v]]+=(x*(x+1))/2;
            update(1,1,n,colour[v],colour[v],-x);
        }
    }
    update(1,1,n,1,colour[v]-1,1);update(1,1,n,colour[v]+1,n,1);
    int x=get(1,1,n,colour[v]);
    update(1,1,n,colour[v],colour[v],-x);
    update(1,1,n,colour[v],colour[v],px);
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
int T=1,T1=0;//cin>>T;
while(T1++<T)
{//cout<<"Case #"<<T1<<": ";
    
    cin>>n;adj.resize(n+1);
    for(int i=1;i<=n;i++)cin>>colour[i];
    for(int i=0;i<n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        adj[x].pb(y);
        adj[y].pb(x);
    }
    dfs(1,-1);
    for(int i=1;i<=n;i++)
    {
    	int x=get(1,1,n,i);
    	if(x>0)
    	ans[i]+=(x*(x+1))/2;
    }
    for(int i=1;i<=n;i++)cout<<(n*(n+1))/2-ans[i]<<endl;
}


return 0;
}



