#include <iostream> 
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

#define For(i , a , b) for (int i = a , _b = b ; i <= _b ; ++i)
#define Ford(i , a  ,b) for (int i = a , _b = b : i >= _b ; --i)
#define Rep(i , n) for (int i = 0 , _n = n ; i < _n ; ++i)
#define sz(A) ((int)A.size())
#define LL(x) (x << 1)
#define RR(x) ((x << 1) | 1)

typedef pair<int , int> pt;

const int maxn = 100000 + 123;
int n, color[maxn];
std::vector<int> V[maxn];

void ReadData() {
	scanf("%d",&n); 
	For(i, 1, n) scanf("%d", &color[i]);
	For(i, 2, n) {
		int u, v; scanf("%d%d", &u, &v); 
		V[u].push_back(v); V[v].push_back(u);
	}
}

long long res[maxn];
bool deleted[maxn];
int chil[maxn];

void dfsSize(const int u, const int prev) {
	chil[u] = 1;
	for (int v: V[u]) if (v != prev && !deleted[v]) {
		dfsSize(v, u); 
		chil[u] += chil[v];
	}
}

int newRoot(const int u, const int prev, const int Size) {
	for (auto v : V[u]) if (v != prev && !deleted[v]) {
		if (chil[v] * 2 >= Size) return newRoot(v, u, Size);
	} 
	return u;
}

long long dp[maxn], sum[maxn]; // number of ways for each color
int was[maxn];
int sta[maxn], top = 0;
long long total = 0;
int newChil[maxn];
int imp[maxn];

void dfsSizeNew(const int u, const int prev) {
	newChil[u] = 1;
	for (int v: V[u]) if (v != prev && !deleted[v]) {
		dfsSizeNew(v, u); 
		newChil[u] += newChil[v];
	}
}

void dfs(const int u, const int prev) {
	sta[++top] = u; 
	if (!was[color[u]]) {
		sum[color[u]] += newChil[u];
		dp[u] += newChil[u];
		imp[u] = newChil[u];
		if (prev) total += newChil[u];
	} else imp[u] = 0;
	was[color[u]]++;
	for (auto v : V[u]) if (v != prev && !deleted[v]) {
		dfs(v, u);
		dp[u] += dp[v];
	}
	was[color[u]]--;
}

int staCol[maxn], top2 = 0, cntCol[maxn];
void preCal(const int u, const int prev) {
	if (!cntCol[color[u]]) {
		cntCol[color[u]] += imp[u]; 
		staCol[++top2] = color[u];
	} else cntCol[color[u]] += imp[u];
	for (auto v : V[u]) if (v != prev && !deleted[v]) {
		preCal(v, u);
	}
}
void resetCol() {
	For(i, 1, top2) {
		cntCol[staCol[i]] = 0;
	}
	top2 = 0;
}

int totalColor;
void dfsCal(const int u, const int prev, const int anc, const int Size) {
	if (!was[color[u]]) {
		++totalColor;
		total -= (sum[color[u]] - cntCol[color[u]]);
	}
	was[color[u]]++;
	res[u] += total;
	res[u] += 1LL * totalColor * (Size - newChil[anc] );

	for (auto v : V[u]) if (v != prev && !deleted[v]) {
		dfsCal(v, u, anc, Size);
	}

	was[color[u]]--;
	if (!was[color[u]]) {
		totalColor--;
		total += (sum[color[u]] - cntCol[color[u]]);
	}
}

void Solve(int root, int Size) {
	total = 0;
	totalColor = 0;

	dfsSize(root, 0);
	int u = newRoot(root, 0, Size);
	dfsSizeNew(u, 0);

	dfs(u, 0);
	res[u] += dp[u];
	deleted[u] = true;
	for (auto v : V[u]) if (!deleted[v]) {
		totalColor = 1; 
		was[color[u]] = 1;
		total = dp[u] - dp[v] - newChil[u];
		resetCol();
		preCal(v, u);
		dfsCal(v, u, v, chil[root]);
	}

	For(i, 1, top) {
		int u = sta[i];
		sum[color[u]] = dp[u] = 0;
		was[color[u]] = 0;
		imp[u] = 0;
	}
	top = 0;
	for (auto v : V[u]) if (!deleted[v]) {
		if (chil[v] < chil[u]) Solve(v, chil[v]); else Solve(v, Size - chil[u]);
	}
}

void Process() {
	Solve(1, n);
	For(i, 1, n) cout << res[i] << "\n";
	//cout << res[5] << endl;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	//freopen("input2.inp" , "r" , stdin);
//	freopen("output.out" , "w" , stdout);
	ReadData();
	Process();

	return 0;

}			