#include<bits/stdc++.h>
using namespace std;
using UL = unsigned long;
using ULL = unsigned long long;
using LL = long long;
#define rep(i, n) for(UL i = 0; i < (n); i++)

struct Problem {

    UL N;
    vector<UL> C;
    vector<vector<UL>> E;
    vector<UL> Sz, P, A, I, CN;
    vector<ULL> ans;

    void DFS(UL p) {
        I[p] = CN[C[p]];
        for (UL e : E[p]) {
            if (P[p] == e) continue;
            P[e] = p;
            DFS(e);

            ULL t = Sz[e] - (A[C[p]] - I[p]);
            ans[C[p]] += t * (t + 1) / 2;
            I[p] += Sz[e];
            A[C[p]] = I[p];
        }
        A[C[p]]++;
        if (p != 0) Sz[P[p]] += Sz[p];
    }

    void Solve() {
        cin >> N;
        C.resize(N); rep(i, N) { cin >> C[i]; C[i]--; }
        E.resize(N);
        rep(i, N - 1) {
            UL u, v; cin >> u >> v; u--; v--;
            E[u].push_back(v);
            E[v].push_back(u);
        }
        Sz.assign(N, 1);
        P.assign(N, -1);
        A.assign(N, 0);
        I.assign(N, 0);
        CN.assign(N, 0);
        ans.assign(N, 0);

        DFS(0);

        rep(i, N) {
            ULL t = N - A[i];
            ans[i] += t * (t + 1) / 2;
            ans[i] = (ULL)N * (N + 1) / 2 - ans[i];
            cout << ans[i] << endl;
        }
    }
};
int main() {
    unique_ptr<Problem> p(new Problem());
    cout << fixed << setprecision(10);
    p->Solve();
    return 0;
}
