#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
#include <stack>
#include <assert.h>
#include <unordered_set>
#include <random>


using namespace std;
using ll = long long;
using ull = unsigned long long;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

#define REP(i, n) for(ll i = 0; i < n; i++)


















int main(){
    ll n;
    cin >> n;
    vector<ll> c(n);
    REP(i, n){
        cin >> c[i];
        c[i]--;
    }
    vector<vector<ll>> g(n);
    REP(i, n - 1){
        ll a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<ll> a(n, n * (n + 1) / 2), d(n);
    ll s = 0;
    auto dfs = [&](auto &&dfs, ll now, ll par) -> void {
        ll tmp = s + d[c[now]];
        s++;
        for(auto &ch : g[now]){
            if(ch == par) continue;
            d[c[now]] = -s;
            dfs(dfs, ch, now);
            a[c[now]] -= (s + d[c[now]]) * (s + d[c[now]] + 1) / 2;
        }
        d[c[now]] = tmp - s;
    };
    dfs(dfs, 0, -1);
    REP(i, n){
        cout << a[i] - (s + d[i]) * (s + d[i] + 1) / 2 << endl;
    }
}