//Link : https://codeforces.com/contest/1253

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200005
/*
#include <limits.h>
INT_MAX
LLONG_MAX
*/

bool vis[N];
int c[N],sz[N];
vector<int> adj[N];

int getSize(int node,int par) {
  sz[node] = 1;
  for(int ch : adj[node]) {
    if(vis[ch] || ch==par) {
      continue;
    }
    getSize(ch,node);
    sz[node] += sz[ch];
  }
  return sz[node];
}

int getCentroid(int allSz,int node,int par) {
  int maxSubSize = allSz - sz[node];
  for(int ch : adj[node]) {
    if(vis[ch] || ch==par) {
      continue;
    }
    maxSubSize = max(maxSubSize, sz[ch]);
    int ret = getCentroid(allSz,ch,node);
    if(ret!=-1) {
      return ret;
    }
  }
  if(maxSubSize*2<= allSz) {
    return node;
  } else {
    return -1;
  }
}

bool isPainted[N];
ll all[N];
void paintAll(int node,int par,int ad) {
  bool painted = false;
  if(!isPainted[c[node]]) {
    painted = true;
    isPainted[c[node]] = true;
    all[c[node]] += ad*sz[node];
  }
  for(int ch : adj[node]) {
    if(vis[ch] || ch==par) {
      continue;
    }
    paintAll(ch,node,ad);
  }
  if(painted) {
    isPainted[c[node]] = false;
  }
}


ll out[N];

void paintColor(int node,int par,ll weight) {
  bool painted = false;
  if(!isPainted[c[node]]) {
    painted = true;
    isPainted[c[node]] = true;
    out[c[node]] += (ll)(weight-all[c[node]])*sz[node];
  }
  for(int ch : adj[node]) {
    if(vis[ch] || ch==par) {
      continue;
    }
    paintColor(ch,node,weight);
  }
  if(painted) {
    isPainted[c[node]] = false;
  }
}

int csz[N];
void solveCentroid(int centroid) {
  int size = getSize(centroid,0);
  out[c[centroid]] += 1;
  int tmpSize = size-1;
  vis[centroid] = true;
  int ad = 1;
  isPainted[c[centroid]] = true;
  for(int ch : adj[centroid]) {
    if(vis[ch]) {
      continue;
    }
    int u = getSize(ch,centroid);
    csz[ch] = u;
    out[c[centroid]] += (ll)u*ad;
    ad += u;
    paintAll(ch,centroid,1);
  }
  for(int ch : adj[centroid]) {
    if(vis[ch]) {
      continue;
    }
    int u = csz[ch];
    paintAll(ch,centroid,-1);
    paintColor(ch,centroid,size - u);
  }
  isPainted[c[centroid]] = false;
  for(int ch : adj[centroid]) {
    if(vis[ch]) {
      continue;
    }
    int size = getSize(ch,centroid);

    int cent = getCentroid(size,ch,centroid);
    assert(cent!=-1);
    solveCentroid(cent);
  }
}

void solve() {
  int n;
  scanf("%d ", &n);
  for(int i=1;i<=n;++i) {
    scanf("%d ", &c[i]);
  }
  for(int i=0;i<n-1;++i) {
    int x,y;
    scanf("%d %d ", &x,&y);
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  int size = getSize(1,0);
  int cent = getCentroid(n,1,0);
  solveCentroid(cent);
  for(int i=1;i<=n;++i) {
    printf("%lld\n", out[i]);
  }
}

int main() {
    //freopen("input.txt","r",stdin);
    solve();
    return 0;
}
