#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T>
struct LinkCutTree{
	struct Node{
		Node *l, *r, *p;
		int sz;
		bool rev;
		T val, dat, sum;
		Node(T val):val(val), dat(0), sum(val), sz(1), rev(false), l(nullptr), r(nullptr), p(nullptr){}
		bool is_root(){
			return !p || (p->l!=this && p->r!=this);
		}
	};

	LinkCutTree(){}
	void toggle(Node *t){
		if(!t) return;
		swap(t->l, t->r);
		t->rev^=true;
	}
	void push(Node *t){
		if(!t) return;
		if(t->rev){
			toggle(t->l);
			toggle(t->r);
			t->rev=false;
		}
	}
	void update(Node *t){
		t->sz=1, t->sum=t->val+t->dat;
		if(t->l) t->sz+=t->l->sz, t->sum+=t->l->sum;
		if(t->r) t->sz+=t->r->sz, t->sum+=t->r->sum;
	}
	void rotate(Node *t){
		Node *p=t->p, *pp=p->p, *ch;
		if(p->l==t) ch=t->r;
		else ch=t->l;
		if(pp){
			if(pp->l==p) pp->l=t;
			else if(pp->r==p) pp->r=t;
		}
		t->p=pp;
		if(p->l==t) t->r=p;
		else t->l=p;
		p->p=t;
		if(p->l==t) p->l=ch;
		else p->r=ch;
		if(ch) ch->p=p;
		update(p);
		update(t);
	}
	int state(Node *t){
		if(t->is_root()) return 0;
		else if(t->p->l==t) return 1;
		else return -1;
	}
	void splay(Node *t){
		push(t);
		while(state(t)){
			if(!state(t->p)){
				push(t->p); push(t);
				rotate(t);
			}else{
				push(t->p->p); push(t->p); push(t);
				if(state(t->p)==state(t)){
					rotate(t->p);
					rotate(t);
				}else{
					rotate(t);
					rotate(t);
				}
			}
		}
	}
	Node *expose(Node *t){
		Node *rp=nullptr;
		for(Node *now=t; now; now=now->p){
			splay(now);
			if(rp) now->dat-=rp->sum;
			if(now->r) now->dat+=now->r->sum;
			now->r=rp;
			update(now);
			rp=now;
		}
		splay(t);
		return rp;
	}
	void link(Node *c, Node *p){//c:root
		expose(c);
		expose(p);
		p->r=c;
		c->p=p;
		update(p);
	}
	void cut(Node *c){//c->l != nullptr
		expose(c);
		c->l->p=nullptr;
		c->l=nullptr;
		update(c);
	}
	void evert(Node *t){
		expose(t);
		toggle(t);
		push(t);
	}
};

int main()
{
    LinkCutTree<int> lct;
	using node=LinkCutTree<int>::Node;
	int n;
	cin>>n;
	vector<int> v[200020];
	for(int i=0; i<n; i++){
		int c; cin>>c;
		v[c-1].push_back(i);
	}
	vector<vector<P>> g(n);
	bool used[200020]={};
	vector<node*> vt(n);
	for(int i=0; i<n; i++) vt[i]=new node(1);
	for(int i=0; i<n-1; i++){
		int a, b;
		cin>>a>>b;
		a--; b--;
		g[a].push_back({b, i});
		g[b].push_back({a, i});
		lct.evert(vt[a]);
		lct.link(vt[a], vt[b]);
	}
	for(int i=0; i<n; i++){
		ll ans=(ll)n*(n+1)/2;
		for(auto x:v[i]){
			for(auto q:g[x]){
				int i=q.second;
				if(used[i]) continue;
				used[i]=1;
				int y=q.first;
				lct.evert(vt[x]);
				ll c=vt[x]->sum;
				lct.cut(vt[y]);
				ll c1=vt[y]->sum;
				ans-=c*(c+1)/2;
				ans+=c1*(c1+1)/2;
				ans+=(c-c1)*(c-c1+1)/2;
			}
		}
		ans-=v[i].size();
		cout<<(ll)n*(n+1)/2-ans<<endl;
		for(auto x:v[i]){
			for(auto q:g[x]){
				int i=q.second;
				if(!used[i]) continue;
				used[i]=0;
				int y=q.first;
				lct.evert(vt[x]);
				lct.link(vt[x], vt[y]);
			}
		}
	}
	return 0;
}


Measured memory_bytes: 38588