#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

template<typename T>
struct BinaryIndexedTree{
    vector<T> data;
    int sz;

    BinaryIndexedTree(int n){
        sz = n + 1;
        data.assign(sz, 0);
    }

    void add(int i, T x){
        ++i;
        while(i < sz){
            data[i] += x;
            i += i & -i;
        }
    }

    // [0, i] の区間和
    T sum(int i){
        T res = 0;
        ++i;
        while(i > 0){
            res += data[i];
            i -= i & -i;
        }
        return res;
    }

    T sum(int l, int r){
        return sum(r - 1) - sum(l - 1);
    }

    // val 以上を満たす 0-indexed の位置を返す
    int lower_bound(T val){
        // if(val <= 0)    return -1;
        int res = 0;
        int n = 1;
        while(n < sz)   n <<= 1;
        for(int k = n >> 1; k > 0; k >>= 1){
            if(res + k < sz && data[res + k] < val){
                val -= data[res + k];
                res += k;
            }
        }
        return res;     // 1-indexed であれば、res + 1 を返す
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector<int> c(n);
    vector<vector<int>> vs(n);
    for(int i = 0; i < n; ++i){
        cin >> c[i];
        vs[c[i] - 1].emplace_back(i);
    }
    vector<vector<int>> graph(n);
    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        graph[u].emplace_back(v);
        graph[v].emplace_back(u);
    }

    vector<int> in(n), out(n);
    int k = 0;
    function<void(int, int)> dfs = [&](int from, int p) -> void {
        in[from] = k++;
        for(int to : graph[from]){
            if(to == p) continue;
            dfs(to, from);
        }
        out[from] = k;
        return;
    };
    dfs(0, -1);

    BinaryIndexedTree<int> BIT(n);
    for(int i = 0; i < n; ++i)  BIT.add(i, 1);
    
    ll total = (ll)n * (n + 1) / 2;
    for(int i = 0; i < n; ++i){
        ll ans = total;
        vector<int>& nvs = vs[i];
        sort(nvs.begin(), nvs.end(), [&](int a, int b){ return in[a] > in[b]; });
        vector<P> history;
        for(int u : nvs){
            int cnt = 1;
            for(int v : graph[u]){
                if(in[v] < in[u])   continue;
                int num = BIT.sum(in[v], out[v]);
                ans -= (ll)num * (num + 1) / 2;
                cnt += num;
            }
            BIT.add(in[u], -cnt);
            history.emplace_back(in[u], cnt);
        }
        int num = BIT.sum(n - 1);
        ans -= (ll)num * (num + 1) / 2;
        for(auto p : history){
            BIT.add(p.first, p.second);
        }
        cout << ans << '\n';
    }
}

Measured memory_bytes: 44536