#include <bits/stdc++.h>

#define ln                '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (int i = 0; i < to; ++i)
#define cont(i, to)        for (int i = 1; i <= to; ++i)
#define circ(i, fm, to)    for (int i = fm; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const int nsz = int(2e5), inf = 0x3f3f3f3f;
int n, rt = 1, a[nsz + 5], siz[nsz + 5], dfn[nsz + 5], mn[nsz + 5];
vector<int> p[nsz + 5], fm[nsz + 5];

struct graph {
	int sz, hd[nsz + 5];
	
	struct edge {
		int nxt, to;
		
		edge(int nxt = 0, int to = 0) {
			this->nxt = nxt, this->to = to;
		}
	} e[2 * nsz + 5];
	
	inline edge& operator [] (int id) { return e[id]; }
	
	void inline link(int u, int v) {
		e[++sz] = edge(hd[u], v), hd[u] = sz;
		e[++sz] = edge(hd[v], u), hd[v] = sz;
	}
};
graph g;

void dfs(int u = rt, int p = -1) {
	// cerr << "dfs: " << u << ln;
	static int t = 0;
	dfn[u] = ++t;
	siz[u] = 1;
	// memset(mn, inf, sizeof(mn));
	int c = a[u], pc = p == -1 ? 0 : a[p], mnc = inf, mnpc = inf;
	for (int i = g.hd[u]; i; i = g[i].nxt) {
		int v = g[i].to;
		if (v == p)  continue;
		dfs(v, u);
		siz[u] += siz[v];
		// cerr << "from: " << v << ": " << mn[c] << ln;
		mnc = min(mnc, mn[c]);
		if (mnc != mnpc)  mnpc = min(mnpc, mn[pc]);
	}
	if (pc && c != pc) {
		mnpc = min(mnpc, (int) ::p[pc].size());
		::p[pc].push_back(u);
		// fm[pc].push_back(mnpc);
	}
	mnc = min(mnc, (int) ::p[c].size());
	::p[c].push_back(u);
	// fm[c].push_back(mnc);
	
	// cerr << u << ": " << c << ' ' << pc << ": " << mnc << '.' << mnpc << ln;
	
	mn[c] = mnc;
	if (c != pc)  mn[pc] = mnpc;
	// cerr << mnc << ' ' << mnpc << ln;
	// cerr << u << ": ";
	// cont (i, n) {
	// 	cerr << (mn[i] == inf ? -1 : mn[i]) << ", ";
	// }
	// cerr << ln;
	// cerr << u << " ended" << ln;
	// mn[c] = mn[pc] = inf;
}

bool inline in(int a, int l, int r) { return a >= l && a <= r; }

num inline f(num a) { return a * (a + 1) / 2; }

num inline solve(int c) {
	// cerr << "solve: " << c << ln;
	static int sum[nsz + 5];
	num res = f(n);
	loop (i, p[c].size()) {
		int u = p[c][i], l = i;
		
		// cerr << "tmp of " << u << ": " << l << ' ' << ((l - 1) >= 0 ? fm[c][l - 1] : -1) << ' ' << ((l - 1) >= 0 ? p[c][fm[c][p[c][l - 1]]] : -1) << ln;
		for (; l - 1 >= 0 && in(dfn[p[c][fm[c][l - 1]]], dfn[u], dfn[u] + siz[u] - 1); l = fm[c][l - 1]) {
			// cerr << "tmp: " << l << ln;
		}
		
		fm[c].push_back(l);
		
		// cerr << u << ' ' << l << ln;
		// loop (i, fm[c].size()) {
		// 	cerr << fm[c][i] << ',';
		// }
		// cerr << ln;
		
		if (a[u] == c) {
			// cerr << l << ln;
			sum[i + 1] = sum[i] + siz[u] - (sum[i] - sum[l]);
		} else {
			sum[i + 1] = sum[i];
			res -= f(siz[u] - (sum[i] - sum[l]));
			// cerr << siz[u] - (sum[i] - sum[l]) << ln;
			// cerr << "res: " << res << ln;
		}
		// cerr << i << ' ' << u << ": " << sum[i + 1] << ln;
	}
	// cerr << ln;
	// cerr << n - sum[p[c].size()] << ln;
	res -= f(n - sum[p[c].size()]);
	// cerr << "res: " << res << ln;
	// cerr << ln;
	return res;
}

int main() {
	scanf("%d", &n);
	cont (i, n) {
		scanf("%d", &a[i]);
	}
	cont (i, n - 1) {
		int u, v;
		scanf("%d%d", &u, &v);
		g.link(u, v);
	}
	memset(mn, inf, sizeof(mn));
	dfs();
	cont (c, n) {
		printf("%lld\n", solve(c));
	}
}

Measured memory_bytes: 50288