#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
template <bool Directed, typename TEdge, bool Index> struct graph {
    using EType = TEdge;
    vector<TEdge> edges;
    vector<vector<int>> adj;
    graph(int n) : adj(n + Index) {}
    graph(int n, int m) : graph(n) { edges.reserve(m << not Directed); }
    TEdge& operator()(int e) { return edges[e]; }
    vector<int>& operator[](int u) { return adj[u]; }
    int size() { return adj.size() - Index; }
    void add_edge(const TEdge& e) {
        adj[e.u].push_back(edges.size());
        edges.push_back(e);
        if(not Directed) {
            adj[e.v].push_back(edges.size());
            edges.push_back(e.reverse());
        }
    }
};
struct edge {
    int u, v;
    edge reverse() const { return {v, u}; }
    friend ostream& operator<<(ostream& os, const edge& e) {
        return os << "(" << e.u << ", " << e.v << ")";
    }
};
template <typename T> using f = function<T>;
template <typename TEdge> struct dfs_params {
    f<void(int)> pre_node = [](int) -> void {};
    f<void(int)> pos_node = [](int) -> void {};
    f<void(TEdge&)> pre_edge = [](TEdge&) -> void {};
    f<void(TEdge&)> pos_edge = [](TEdge&) -> void {};
    int root = 0;
};
template <bool Directed, typename TEdge, bool Index, typename TParamsetter = f<void(dfs_params<TEdge>&)>>
vector<int> dfs(graph<Directed, TEdge, Index>& g, const TParamsetter& psetter = [](dfs_params<TEdge>&) {}) {
    static_assert(is_convertible<decltype(psetter), f<void(dfs_params<TEdge>&)>>::value, "psetter must be void(dfs_params<TEdge>&)");
    dfs_params<TEdge> params;
    psetter(params);
    vector<int> component(g.adj.size());
    function<void(int, int)> dfs = [&](int u, int c) {
        component[u] = c;
        params.pre_node(u);
        for(int e : g[u]) if(not component[g(e).v]) {
            params.pre_edge(g(e)), dfs(g(e).v, c), params.pos_edge(g(e));
        }
        params.pos_node(u);
    };
    if(params.root) {
        dfs(params.root, 1);
    } else {
        for(int u = Index, c = 0; u < g.adj.size(); u++) if(not component[u]) {
            dfs(u, ++c);
        }
    }
    return component;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> c(n + 1);
    for(int i = 1; i <= n; i++) {
        cin >> c[i];
    }
    graph<0, edge, 1> g(n, n - 1);
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g.add_edge({u, v});
    }
    auto choose2 = [](ll n) { return n * (n + 1) / 2; };
    vector<int> size(n + 1, 1), orig_size(n + 1, 1), size_above(n + 1, n);
    size_above[c[1]] = 0;
    vector<vector<int>> child_of_nearest_ancestor_of_color(n + 1);
    vector<ll> ans(n + 1, choose2(n));
    dfs(g, [&](auto& p) {
        p.pre_edge = [&](edge& e) {
            child_of_nearest_ancestor_of_color[c[e.u]].push_back(e.v);
        };
        p.pos_edge = [&](edge& e) {
            size[e.u] += orig_size[e.v];
            orig_size[e.u] += orig_size[e.v];
            if(child_of_nearest_ancestor_of_color[c[e.v]].empty()) {
                size_above[c[e.v]] -= orig_size[e.v];
            } else {
                size[child_of_nearest_ancestor_of_color[c[e.v]].back()] -= orig_size[e.v];
            }
            ans[c[e.u]] -= choose2(size[e.v]);
            child_of_nearest_ancestor_of_color[c[e.u]].pop_back();
        };
    });
    for(int i = 1; i <= n; i++) {
        cout << ans[i] - choose2(size_above[i]) << endl;
    }
    return 0;
}


Measured memory_bytes: 53216