#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 5;
int sz[N], c[N], lst[N], h[N];
long long ans[N], f[N], val[N], s[N];
vector <int> g[N];

void dfs(int v, int p = 0) {
    int prv = lst[c[v]];
    for (int u : g[v]) if (u != p) lst[c[v]] = u, dfs(u, v), ans[c[v]] += 1ll * sz[v] * sz[u], sz[v] += sz[u];
    lst[c[v]] = prv, sz[v]++;
    if (prv > 1) val[prv] += sz[v];
    else s[c[v]] += sz[v];
    return ;
}
void Solve(int v, int p = 0) {
    int prv = lst[c[v]];
    ans[c[v]] += 1ll * sz[v] * (sz[prv] - sz[v] + 1);
    if (prv > 1) f[c[v]] += 1ll * sz[v] * (val[prv] - sz[v]);
    else f[c[v]] += 1ll * sz[v] * (s[c[v]] - sz[v]);
    for (int u : g[v]) if (u != p) lst[c[v]] = u, Solve(u, v);
    lst[c[v]] = prv;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> c[i];
    for (int i = 0; i < n - 1; i++) {
        int u, v; cin >> u >> v;
        g[u].push_back(v), g[v].push_back(u);
    }
    fill(lst, lst + N, 1), dfs(1), fill(lst, lst + N, 1), Solve(1);
    for (int i = 1; i <= n; i++) cout << ans[i] - (f[i]/2) << "\n";


    return 0;
}
