#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
    return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
template<class T> inline void YES(T condition) { 
    if(condition) cout << "YES" << endl;
    else cout << "NO" << endl;
}
template<class T> inline void Yes(T condition) {
    if(condition) cout << "Yes" << endl;
    else cout << "No" << endl;
}
template<class T> T gcd(T a, T b) {
    if(a && b) { return gcd(min(a, b), max(a, b) % min(a, b)); }
    else { return a; }
}
template<class T> T lcm(T a, T b){
    return a / gcd(a, b) * b;
}
const LL MOD = 2019;
long long qpow(long long a, long long b) {
    if (b == 0) return 1;
    long long res = 1;
    while (b) {
        if (b & 1) res = (res * a) % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> color(n + 1);
    vector<vector<int>> nei(n + 1);
    for (int i = 1; i <= n; i++) cin >> color[i];
    for (int i = 0; i < n - 1; i++) {
        int u, v; 
        cin >> u >> v;
        nei[u].push_back(v);
        nei[v].push_back(u);
    }

    vector<int> trsz(n + 1, 1);
    function<void(int, int)> dfs = [&](int u, int fa) {
        for (int& v: nei[u]) {
            if (v == fa) continue;
            dfs(v, u);
            trsz[u] += trsz[v];
        }
    };
    dfs(1, -1);
    // for (int i = 1; i <= n; i++) cout << i << ':' << trsz[i] << '\n';

    vector<LL> res(n + 1, 1LL * n * (n + 1) / 2);
    vector<int> ccnt(n + 1, 0);
    int finish = 0;
    function<void(int, int)> dfs2 = [&](int u, int fa) {
        int cu = color[u];
        LL bfr = ccnt[cu];
        for(int& v: nei[u]) {
            if (v == fa) continue;
            LL pre = finish - ccnt[cu];
            dfs2(v, u);
            LL nxt = finish - ccnt[cu];
            LL minus = nxt - pre;
            res[cu] -= minus * (minus + 1) / 2;
            // cout << u << "&" << v << ":" << trsz[v] << '-' << ccnt[cu] << endl;
        }
        ccnt[cu] = bfr + trsz[u];
        finish++;
    };
    dfs2(1, -1);

    for (int c = 1; c <= n; c++) {
        LL minus = trsz[1] - ccnt[c];
        res[c] -= minus * (minus + 1) / 2;
    }

    for (int c = 1; c <= n; c++) cout << res[c] << '\n';

    return 0;
}


Measured memory_bytes: 39876