#include <iostream>
#include <vector>

using namespace std;

long long n;
vector <int> c;
vector < vector <int> > mv;
vector <long long> res;

int sol(int ac, int fa,vector <int> &el){
	long long gu = el[c[ac]];
	long long so = 1;
	long long re;
	for(int i=0;i<mv[ac].size();i++)if(mv[ac][i]!=fa){
		el[c[ac]]=0;
		re = sol(mv[ac][i],ac,el);
		so += re;
		re-=el[c[ac]];
		re = re * (re-1)/2 + re;
		res[c[ac]] -= re;
	}
	el[c[ac]] = gu +so;
	return so;
}
int main(){
	
	cin>>n;
	c.resize(n);
	for(int i=0;i<n;i++)cin>>c[i],c[i]--;
	mv.resize(n);
	res.resize(n,n*(n-1)/2+n);
	vector <int> el(n,0);
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		u--;v--;
		mv[v].push_back(u);
		mv[u].push_back(v);
	}
	sol(0,0,el);
	for(int i=0;i<n;i++){
		long long re = n - el[i];
		re = re * (re-1)/2 + re;
		res[i] -= re;
		cout<<res[i]<<endl;
	}
	
	return 0;
}