#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

ll calc(ll x){ return (x*(x+1))/2;}

vvl G = VV(300000, 0, 0, ll);
vll c(300000, 0);
vll sz(300000, 0);
vector<ll> par(300000, -1);
vll root(300000, -1);
vvl T = VV(400001, 0, 0, ll);

template< typename G >
struct HeavyLightDecomposition {
  G &g;
  vector< int > sz, in, out, head, rev, par;
  HeavyLightDecomposition(G &g) :
      g(g), sz(g.size()), in(g.size()), out(g.size()), head(g.size()), rev(g.size()), par(g.size()) {}

  void dfs_sz(int idx, int p) {
    par[idx] = p;
    sz[idx] = 1;
    if(g[idx].size() && g[idx][0] == p) swap(g[idx][0], g[idx].back());
    for(auto &to : g[idx]) {
      if(to == p) continue;
      dfs_sz(to, idx);
      sz[idx] += sz[to];
      if(sz[g[idx][0]] < sz[to]) swap(g[idx][0], to);
    }
  }

  void dfs_hld(int idx, int par, int &times) {
    in[idx] = times++;
    rev[in[idx]] = idx;
    for(auto &to : g[idx]) {
      if(to == par) continue;
      head[to] = (g[idx][0] == to ? head[idx] : to);
      dfs_hld(to, idx, times);
    }
    out[idx] = times;
  }

  void build() {
    dfs_sz(0, -1);
    int t = 0;
    dfs_hld(0, -1, t);
  }

  int la(int v, int k) {
    while(1) {
      int u = head[v];
      if(in[v] - k >= in[u]) return rev[in[v] - k];
      k -= in[v] - in[u] + 1;
      v = par[u];
    }
  }

  int lca(int u, int v) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) return u;
    }
  }

  template< typename T, typename Q, typename F >
  T query(int u, int v, const T &ti, const Q &q, const F &f, bool edge = false) {
    T l = ti, r = ti;
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v), swap(l, r);
      if(head[u] == head[v]) break;
      l = f(q(in[head[v]], in[v] + 1), l);
    }
    return f(f(q(in[u] + edge, in[v] + 1), l), r);
//  return {f(q(in[u] + edge, in[v] + 1), l), r};
  }

  template< typename Q >
  void add(int u, int v, const Q &q, bool edge = false) {
    for(;; v = par[head[v]]) {
      if(in[u] > in[v]) swap(u, v);
      if(head[u] == head[v]) break;
      q(in[head[v]], in[v] + 1);
    }
    q(in[u] + edge, in[v] + 1);
  }
};
HeavyLightDecomposition<vvl> hld(G);
vll dep(400001, 0);
ll init(ll now, ll from, ll d){
  dep[now] = d;
  if(par[c[now]]==-1) {
    if(now==0) root[c[now]] = now;
    else {
      ll x = 200000 + c[now];
      root[c[now]] = x;
      par[c[now]] = x;
      T[x].push_back(now);
    }
  }else{
    ll p = par[c[now]];
    T[p].push_back(now);
  }
  ll ret = 0;
  ll p = par[c[now]];
  par[c[now]] = now;
  for(auto to:G[now]){
    if(to==from) continue;
    ret += init(to, now, d+1);
  }
  par[c[now]] = p;
  sz[now] = ret + 1;
  return sz[now];
}
vll ans(200001, 0);

ll n;
void dfs(ll now, ll from, ll color){
  if(now>200000){
    ll ret = n;
    for(auto to:T[now]){
      ret -= sz[to];
      dfs(to, now, color);
    }
    ans[color] += calc(ret);
  }else{
    map<ll, ll> mae;
    for(auto to:T[now]){
      ll a = dep[now], b = dep[to];
      ll x = hld.la(to, b-a-1);
      ll num = sz[to];
      auto itr = mae.find(x);
      if(itr==mae.end()) mae.emplace(x, num);
      else (*itr).second += num;
    }
    for(auto to:G[now]){
      //if(c[now]==7) std::cout << now << " " << to << '\n';
      if(dep[to]<dep[now]) continue;
      auto itr = mae.find(to);
      ll num = (itr==mae.end()?0:mae.at(to));
      //if(c[now]==7) std::cout << now << " " << num << '\n';
      ans[color] += calc(sz[to] - num);
    }
    for(auto to:T[now]) dfs(to, now, color);
  }
}

int main(int argc, char const *argv[]) {
  std::cin >> n;
  re(i, n) scanf("%lld", &c[i]);
  re(i, n-1){
    ll a, b;scanf("%lld %lld", &a, &b);
    G[a-1].push_back(b-1);
    G[b-1].push_back(a-1);
  }
  hld.build();
  init(0, -1, 0);
  ll MAX = (n*(n+1))/2;
  for(ll i=1;i<=n;i++) {
    if(root[i]!=-1) dfs(root[i], -1, i);
    else ans[i] = MAX;
  }
  for(ll i=1;i<=n;i++) std::cout << MAX - ans[i] << '\n';
  return 0;
}


Measured memory_bytes: 75636