#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 5;
int n;
vector<int> v[maxn];
int cnt_son[maxn];
#define ll long long 
ll ans[maxn];

ll cal(ll x) {
    return x* (x + 1) / 2;
}

int col[maxn];
int num[maxn];

void dfs(int x, int pre) {
    cnt_son[x] = 1;
    int c = col[x];
    int temp = num[c];
    for (auto &y : v[x]) {
        if (y != pre) {
            int now = num[c];
            dfs(y, x);
            now = num[c] - now;
            ans[c] -= cal(cnt_son[y] - now);
            cnt_son[x] += cnt_son[y];
        }
    }
    num[c] = temp + cnt_son[x];
    return ;
}


int main() {
    cin >> n;
    int x, y;
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &col[i]);
    }
    for (int i = 1; i < n; ++i) {
        scanf("%d %d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
    }
    for (int i = 1; i <= n; ++i) {
        ans[i] = cal(n);
    }
    dfs(1, 0);
    for (int i = 1; i <= n; ++i) {
        ans[i] -= cal(n -num[i]);
        printf("%lld\n", ans[i]);
    }
}