#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    ll n;
    cin >> n;
    vector<ll> ans(n, n*(n-1)/2);
    vector<int> sub(n, 1), cnt(n);
    vector<int> C(n);
    for (auto &&i : C) scanf("%d", &i), i--, ans[i]++;
    vector<vector<int>> G(n);
    for (int i = 0; i < n-1; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    int up = 0;
    auto dfs = [&](int i, int par, auto &&f) -> void {
        int tmp = cnt[C[i]];
        for (auto &&j : G[i]) {
            if(j == par) continue;
            ll x = -(up-cnt[C[i]]);
            f(j, i, f);
            sub[i] += sub[j];
            x += up-cnt[C[i]];
            ans[C[i]] -= x*(x-1)/2;
        }
        up++;
        cnt[C[i]] = tmp+sub[i];
    };
    dfs(0, -1, dfs);
    cout << cnt[0] << "\n";
    for (int i = 0; i < n; ++i){
        printf("%lld\n", ans[i]-(n-cnt[i])*(n-cnt[i]-1)/2); 
    }
    return 0;
}