#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <list>
#include <math.h>
#include <bitset>
#include <iterator>
#include <map>
#include <iomanip>
#include <functional>
#include <string>
#include <algorithm>
#include <queue>
#include <random>
#include <numeric>
#include <set>
#include <cassert>
#include <cmath>
#include <cinttypes>

using namespace std;

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os;
}
template<class T> istream& operator>>(istream& is, vector<T>& v) {
	for (auto i = begin(v); i != end(v); i++) is >> *i; return is;
}
template<class T> T read_val() { throw 1; }
template<> int read_val() { int tmp; scanf("%" PRId32, &tmp); return tmp; }
#define read_int read_val<int>
template<> long long read_val() { long long tmp; scanf("%" PRId64, &tmp); return tmp; }
template<class T> void scanf_vec(vector<T>& vec) { for (int i = 0; i < vec.size(); i++) vec[i] = read_val<T>(); }

#define MOD 1000000007
#define inf 2000000000
#define FWT_SIZE 2000000
#define pii pair<int,int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

vector<vector<int>> G, children, base_children;
vector<int> colors;
vector<int> time_in, time_out;
vector<long long> sizes;

int dfs1(int i)
{
	static int time = 0;
	if (time_in[i] >= 0)
		return 0;

	time_in[i] = time; time++;
	sizes[i] = 1;
	for (int u : G[i])
	{
		sizes[i] += dfs1(u);
	}
	time_out[i] = time; time++;
	return sizes[i];
}

void dfs2(int i, int prev, vector<int> & prev_ids)
{
	int col = colors[i];
	int prev_val = prev_ids[col];
	if (prev_val >= 0)
		children[prev_val].push_back(i);
	else
		base_children[col].push_back(i);
	prev_ids[col] = i;
	vector<int> only_children;
	for (int u : G[i])
	{
		if (u == prev) continue;
		only_children.push_back(u);
		dfs2(u, i, prev_ids);
	}
	G[i] = only_children;
	prev_ids[col] = prev_val;
}

bool is_lte(int u, int v)
{
	return time_in[u] >= time_in[v] && time_out[u] <= time_out[v];
}

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n = read_int();
	colors.resize(n);

	vector<int> color_cnts(n+1, 0);
	scanf_vec(colors);
	for (int c : colors) color_cnts[c]++;

	G.resize(n);
	children.resize(n);

	for (int i = 0; i < n - 1; i++)
	{
		int u = read_int(), v = read_int();
		u--, v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}

	time_in.resize(n,-1);
	time_out.resize(n);
	sizes.resize(n);
	base_children.resize(n + 1);

	dfs1(0);
	vector<int> tmp(n+1, -1);
	dfs2(0, -1, tmp);

	vector<long long> acc_noncolor(n+1, 0);
	for (int i = 0; i < n; i++)
	{
		int j = 0;
		vector<long long> _sizes(G[i].size(), 0);
		for (int u : children[i])
		{
			while (!is_lte(u, G[i][j])) j++;
			_sizes[j] += sizes[u];
		}
		for (int j = 0; j < _sizes.size(); j++)
		{
			long long rem = sizes[G[i][j]] - _sizes[j];
			acc_noncolor[colors[i]] += (rem * (rem + 1LL)) / 2LL;
		}
	}

	long long total = ((long long)n * (n + 1LL)) / 2LL;
	for (int i = 1; i < color_cnts.size(); i++)
	{
		long long sub = n;
		for (int u : base_children[i])
			sub -= sizes[u];
		sub = (sub * (sub + 1LL)) / 2LL;
		if (color_cnts[i] == 0)
			printf("0\n");
		else
		{
			printf("%" PRId64 "\n", total - acc_noncolor[i] - sub);
		}
	}

	return 0;
}

Measured memory_bytes: 68144