#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

using CostType = int;
struct Edge {
  int src, dst; CostType cost;
  Edge(int src, int dst, CostType cost = 0) : src(src), dst(dst), cost(cost) {}
  inline bool operator<(const Edge &x) const {
    return cost != x.cost ? cost < x.cost : dst != x.dst ? dst < x.dst : src < x.src;
  }
  inline bool operator<=(const Edge &x) const { return !(x < *this); }
  inline bool operator>(const Edge &x) const { return x < *this; }
  inline bool operator>=(const Edge &x) const { return !(*this < x); }
};

struct HLD {
  vector<int> parent, subtree, id, inv, head;
  vector<CostType> cost;

  HLD(const vector<vector<Edge>> &graph, int root = 0) : graph(graph) {
    int n = graph.size();
    parent.assign(n, -1);
    subtree.assign(n, 1);
    id.resize(n);
    inv.resize(n);
    head.resize(n);
    dfs1(root);
    head[root] = root;
    int now_id = 0;
    dfs2(root, now_id);
  }

  void v_update(int u, int v, function<void(int, int)> f) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      f(max(id[head[v]], id[u]), id[v] + 1);
      if (head[u] == head[v]) return;
      v = parent[head[v]];
    }
  }

  template <typename T>
  T v_query(int u, int v, function<T(int, int)> f, function<T(T, T)> g, const T UNITY) {
    T left = UNITY, right = UNITY;
    while (true) {
      if (id[u] > id[v]) {
        swap(u, v);
        swap(left, right);
      }
      left = g(left, f(max(id[head[v]], id[u]), id[v] + 1));
      if (head[u] == head[v]) break;
      v = parent[head[v]];
    }
    return g(left, right);
  }

  void subtree_v_update(int ver, function<void(int, int)> f) { f(id[ver], id[ver] + subtree[ver]); }

  template <typename T>
  T subtree_v_query(int ver, function<T(int, int)> f) { return f(id[ver], id[ver] + subtree[ver]); }

  void e_update(int u, int v, function<void(int, int)> f) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      if (head[u] == head[v]) {
        f(id[u], id[v]);
        break;
      } else {
        f(id[head[v]] - 1, id[v]);
        v = parent[head[v]];
      }
    }
  }

  template <typename T>
  T e_query(int u, int v, function<T(int, int)> f, function<T(T, T)> g, const T UNITY) {
    T left = UNITY, right = UNITY;
    while (true) {
      if (id[u] > id[v]) {
        swap(u, v);
        swap(left, right);
      }
      if (head[u] == head[v]) {
        left = g(left, f(id[u], id[v]));
        break;
      } else {
        left = g(left, f(id[head[v]] - 1, id[v]));
        v = parent[head[v]];
      }
    }
    return g(left, right);
  }

  void subtree_e_update(int ver, function<void(int, int)> f) { f(id[ver], id[ver] + subtree[ver] - 1); }

  template <typename T>
  T subtree_e_query(int ver, function<T(int, int)> f) { return f(id[ver], id[ver] + subtree[ver] - 1); }

  int lca(int u, int v) {
    while (true) {
      if (id[u] > id[v]) swap(u, v);
      if (head[u] == head[v]) return u;
      v = parent[head[v]];
    }
  }

private:
  vector<vector<Edge>> graph;

  void dfs1(int ver) {
    for (Edge &e : graph[ver]) {
      if (e.dst != parent[ver]) {
        parent[e.dst] = ver;
        dfs1(e.dst);
        subtree[ver] += subtree[e.dst];
        if (subtree[e.dst] > subtree[graph[ver].front().dst]) swap(e, graph[ver].front());
      }
    }
  }

  void dfs2(int ver, int &now_id) {
    id[ver] = now_id++;
    inv[id[ver]] = ver;
    for (const Edge &e : graph[ver]) {
      if (e.dst != parent[ver]) {
        head[e.dst] = (e.dst == graph[ver].front().dst ? head[ver] : e.dst);
        cost.emplace_back(e.cost);
        dfs2(e.dst, now_id);
      }
    }
  }
};

template <typename Abelian>
struct BITRangeAdd {
  BITRangeAdd(int n_, const Abelian UNITY = 0) : n(n_), UNITY(UNITY) {
    ++n;
    dat_const.assign(n, UNITY);
    dat_linear.assign(n, UNITY);
  }

  void add(int left, int right, Abelian val) {
    if (right < ++left) return;
    for (int i = left; i < n; i += i & -i) {
      dat_const[i] -= val * (left - 1);
      dat_linear[i] += val;
    }
    for (int i = right + 1; i < n; i += i & -i) {
      dat_const[i] += val * right;
      dat_linear[i] -= val;
    }
  }

  Abelian sum(int idx) {
    Abelian res = UNITY;
    for (int i = idx; i > 0; i -= i & -i) res += dat_linear[i];
    res *= idx;
    for (int i = idx; i > 0; i -= i & -i) res += dat_const[i];
    return res;
  }

  Abelian sum(int left, int right) {
    if (right <= left) return UNITY;
    return sum(right) - sum(left);
  }

  Abelian operator[](const int idx) { return sum(idx, idx + 1); }

private:
  int n;
  const Abelian UNITY;
  vector<Abelian> dat_const, dat_linear;
};

int main() {
  int n; cin >> n;
  vector<int> c(n); REP(i, n) cin >> c[i], --c[i];
  vector<ll> ans(n, 1LL * n * (n - 1) / 2);
  vector<int> exist(n, 0);
  REP(i, n) ++exist[c[i]];
  REP(i, n) {
    if (exist[i] == 0) {
      ans[i] = 0;
    } else {
      ans[i] += exist[i];
    }
  }
  // REP(i, n) cout << ans[i] << " \n"[i + 1 == n];
  vector<vector<Edge>> graph(n);
  REP(_, n - 1) {
    int a, b; cin >> a >> b; --a; --b;
    graph[a].emplace_back(a, b, 1);
    graph[b].emplace_back(b, a, 1);
  }
  vector<int> par(n, -1), sub(n, 1), depth(n, 0);
  function<void(int)> dfs = [&](int ver) {
    for (const Edge &e : graph[ver]) {
      if (e.dst == par[ver]) continue;
      par[e.dst] = ver;
      depth[e.dst] = depth[ver] + 1;
      dfs(e.dst);
      sub[ver] += sub[e.dst];
    }
  };
  dfs(0);
  vector<vector<int>> vers(n);
  REP(i, n) vers[c[i]].emplace_back(i);
  HLD hld(graph);
  BITRangeAdd<int> bit(n);
  REP(i, n) {
    if (vers[i].empty()) continue;
    sort(ALL(vers[i]), [&](int a, int b) { return depth[a] > depth[b]; });
    // cout << i << ": ";
    // for (int e : vers[i]) cout << e << ' ';
    // cout << endl;
    for (int ver : vers[i]) {
      // cout << ver << '\n';
      int cnt = 0;
      for (const Edge &e : graph[ver]) {
        if (e.dst == par[ver]) continue;
        int v = sub[e.dst] - hld.subtree_v_query<int>(e.dst, [&](int a, int b) { return bit.sum(a, b); });
        // cout << ver << ' ' << v << '\n';
        ans[i] -= 1LL * v * (v - 1) / 2;
        cnt += v;
      }
      bit.add(hld.id[ver], hld.id[ver] + 1, cnt + 1);
    }
    // REP(i, n) cout << bit[i] << " \n"[i + 1 == n];
    if (vers[i].back() != 0) {
      int v = n - bit.sum(0, n);
      // cout << i << ' ' << bit.sum(0, n) << '\n';
      ans[i] -= 1LL * v * (v - 1) / 2;
    }
    for (int e : vers[i]) bit.add(hld.id[e], hld.id[e] + 1, -bit[hld.id[e]]);
    // REP(i, n) cout << bit[i] << " \n"[i + 1 == n];
  }
  REP(i, n) cout << ans[i] << '\n';
  return 0;
}


Measured memory_bytes: 64144