#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int inf = 1000000009;
int rd() {
  char c = getchar();
  int t = 0, f = 1;
  while (!isdigit(c)) f = (c == '-') ? -1 : 1, c = getchar();
  while (isdigit(c)) t = t * 10 + c - 48, c = getchar();
  return t * f;
}
void wt(long long x) {
  if (x < 0)
    putchar('-'), wt(-x);
  else {
    if (x > 9) wt(x / 10);
    putchar(x % 10 + 48);
    return;
  }
}
struct Edge {
  int to, nxt;
} E[N << 1];
int num[N], Max[N], son[N], c[N];
long long Ans[N];
int n, cnt, L[N], R[N], Dfn, bk[N], sz[N], head[N];
void Link(int u, int v) {
  E[++cnt] = (Edge){v, head[u]};
  head[u] = cnt;
  E[++cnt] = (Edge){u, head[v]};
  head[v] = cnt;
}
void Dfs(int x, int fa) {
  sz[x] = 1;
  bk[L[x] = ++Dfn] = x;
  for (int i = head[x]; i; i = E[i].nxt)
    if (E[i].to != fa) {
      Dfs(E[i].to, x);
      sz[x] += sz[E[i].to];
      if (sz[E[i].to] > sz[son[x]]) son[x] = E[i].to;
    }
  R[x] = Dfn;
  return;
}
void Sol(int x, int fa, int k) {
  for (int i = head[x]; i; i = E[i].nxt)
    if (E[i].to != fa && E[i].to != son[x]) Sol(E[i].to, x, 0);
  if (son[x]) Sol(son[x], x, 1);
  Max[x] = Max[son[x]], Ans[x] = Ans[son[x]];
  for (int y, i = head[x]; i; i = E[i].nxt)
    if (E[i].to != fa && E[i].to != son[x]) {
      y = E[i].to;
      for (int j = ((int)L[y]); j <= ((int)R[y]); j++) {
        num[c[bk[j]]]++;
        if (num[c[bk[j]]] > Max[x])
          Max[x] = num[c[bk[j]]], Ans[x] = c[bk[j]];
        else if (num[c[bk[j]]] == Max[x])
          Ans[x] += c[bk[j]];
      }
    }
  num[c[x]]++;
  if (num[c[x]] > Max[x])
    Max[x] = num[c[x]], Ans[x] = c[x];
  else if (num[c[x]] == Max[x])
    Ans[x] += c[x];
  if (!k)
    for (int i = ((int)L[x]); i <= ((int)R[x]); i++) num[c[bk[i]]] = 0;
  return;
}
int main() {
  n = rd();
  for (int i = ((int)1); i <= ((int)n); i++) c[i] = rd();
  for (int i = ((int)1); i <= ((int)n - 1); i++) Link(rd(), rd());
  Dfs(1, -1);
  Sol(1, -1, 1);
  for (int i = ((int)1); i <= ((int)n); i++) wt(Ans[i]), putchar(32);
  return 0;
}
