#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;
}
I2luY2x1ZGUgPGlvc3RyZWFtPiAKI2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGNzdHJpbmc+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBGb3IoaSAsIGEgLCBiKSBmb3IgKGludCBpID0gYSAsIF9iID0gYiA7IGkgPD0gX2IgOyArK2kpCiNkZWZpbmUgRm9yZChpICwgYSAgL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