#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
//using namespace __gnu_pbds;

#define LL       long long
#define PR       pair<LL, LL>
#define READ(f)  freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)

const int N = 3e5 + 5;
const int MOD = 1e9 + 7;
const double PI = 2 * acos(0.0);
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int color[N], lev[N], sub[N];
vector<int> go[N], g[N];

void dfs(int u, int p, int h) {
    lev[u] = h;
    int tmp = 1;
    for (auto v : g[u]) if (v != p) {
        dfs(v, u, h + 1);
        tmp += sub[v];
    }
    sub[u] = tmp;
}

bool cmp(int x, int y) {
    return lev[x] < lev[y];
}

int main() {
//    ios::sync_with_stdio(false);
//    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> color[i];
        go[color[i]].push_back(i);
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int start = 1;
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 1) {
            start = i;
            break;
        }
    }
    dfs(start, 0, 1);
    for (int i = 1; i <= n; i++) {
        sort(go[i].begin(), go[i].end(), cmp);
    }
    for (int i = 1; i <= n; i++) {
        LL cnt = 0, ans = 0;
//        cout << "COLOR: " << i << endl;
        for (auto j : go[i]) {
            LL up = n - sub[j] + 1 - cnt;
            LL down = sub[j];
            LL tmp = up * down;
            ans += tmp;
//            cout << "UP: " << up << "  DOWN: " << down << "  NODE: " << j << endl;
            cnt += up;
        }
        cout << ans << '\n';
    }
    return 0;
}
