#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;

// chmax, chmin
template <class T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
function<T(T, T)> op_max = [](T a, T b) -> T { return max(a, b); };
template <typename T>
function<T(T, T)> op_min = [](T a, T b) -> T { return min(a, b); };

template <typename T>
function<T(T, T)> op_sum = [](T a, T b) -> T { return a + b; };

using namespace std;
//********************  dumps ************************//

template <typename T>
void dump(const T &data, vector<int> &iter)
{
  cout << data << " ";
}
template <typename T>
void dump(const vector<T> &data, vector<int> &iter)
{
  for (auto elem : data)
    dump(elem, iter);
  cout << endl;
}

template <typename T>
void dump(const vector<vector<T>> &data, vector<int> &iter)
{
  for (auto elem : iter)
  {
    cout << "[" << elem << "]";
  }
  cout << endl;
  for (int i = 0; i < data.size(); i++)
  {
    iter.push_back(i);
    dump(data[i], iter);
    iter.pop_back();
  }
  cout << endl;
}

template <typename T>
void dump(const vector<T> &data, int dummy)
{
  for (int i = 0; i < data.size(); i++)
  {
    cout << "[" << i << "] " << data[i] << endl;
  }
}
template <typename T>
void dump(const T &data)
{ // T : data, U = base type val.
  vector<int> iter;
  dump(data, iter);
}
///////////////////////////////////////////////

void solve(long long N, std::vector<long long> C, std::vector<long long> A, std::vector<long long> B)
{
  for (auto &&a : A)
    a--;
  for (auto &&b : B)
    b--;
  for (auto &&c : C)
    c--;

  vector<vector<LL>> edge(N);
  for (int i = 0; i < N - 1; i++)
  {
    LL v = A[i];
    LL u = B[i];
    edge[v].push_back(u);
    edge[u].push_back(v);
  }
  vector<int> used(N, 0);
  vector<LL> preaddpos(N);
  vector<LL> addpos(N);
  vector<LL> ans(N, N * (N + 1) / 2);
  vector<LL> erased(2 * N, 0LL);
  vector<LL> childs(N, 0LL);

  for (int i = 0; i < N; i++)
    addpos[i] = i + N;

  stack<int> sta;
  sta.push(0);
  LL pre_child = 0;
  while (!sta.empty())
  {
    int cur = sta.top();
    sta.pop();
    if (cur < N)
    {
      used[cur] = true;
      sta.push(cur + 2 * N);
      for (auto v : edge[cur])
        if (!used[v])
        {
          sta.push(cur + N);
          sta.push(v);
        }

      LL color = C[cur];
      preaddpos[cur] = addpos[color];
      addpos[color] = cur;
      continue;
    }
    if (cur < 2 * N)
    {
      cur -= N;
      //cout << cur << " " << pre_child << " " << erased[cur] << endl;
      LL m = pre_child - erased[cur];
      LL color = C[cur];
      ans[color] -= m * (m + 1) / 2;
      erased[cur] = 0;

      continue;
    }
    cur -= 2 * N;
    for (auto v : edge[cur])
    {
      childs[cur] += childs[v];
    }
    childs[cur]++;

    LL color = C[cur];
    pre_child = childs[cur];
    addpos[color] = preaddpos[cur];
    LL pv = addpos[color];
    erased[pv] += childs[cur];
    //cout << "a " << cur << " " << pv << " " << erased[pv] << endl;
  }
  for (int i = 0; i < N; i++)
  {
    if (C[0] == i)
      continue;
    int block = i + N;
    LL m = N - erased[block];
    ans[i] -= m * (m + 1) / 2;
    //cout << i << " " << m << endl;
  }

  for (int i = 0; i < N; i++)
  {
    cout << ans[i] << endl;
  }
}

int main()
{
  cout << setprecision(10);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> c(N);
  for (int i = 0; i < N; i++)
  {
    scanf("%lld", &c[i]);
  }
  std::vector<long long> a(N - 1);
  std::vector<long long> b(N - 1);
  for (int i = 0; i < N - 1; i++)
  {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
  }
  solve(N, std::move(c), std::move(a), std::move(b));
  return 0;
}


Measured memory_bytes: 32144