#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
int head[maxn], n, siz[maxn], son[maxn];
int col[maxn], cnt[maxn];
long long ans[maxn], sum, maxc;
int tot = 0, flag;
struct node {
  int next, v;
} ed[maxn * 2];
void add(int u, int v) {
  ed[tot].v = v;
  ed[tot].next = head[u];
  head[u] = tot++;
}
void dfs(int u, int fa) {
  siz[u] = 1;
  for (int i = head[u]; ~i; i = ed[i].next) {
    int v = ed[i].v;
    if (v == fa) continue;
    dfs(v, u);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
  }
}
void count(int u, int fa, int val) {
  cnt[col[u]] += val;
  if (cnt[col[u]] > maxc) {
    maxc = cnt[col[u]];
    sum = col[u];
  } else if (cnt[col[u]] == maxc)
    sum += col[u];
  for (int i = head[u]; ~i; i = ed[i].next) {
    int v = ed[i].v;
    if (v == fa || v == flag) continue;
    count(v, u, val);
  }
}
void dfs2(int u, int fa, bool keep) {
  for (int i = head[u]; ~i; i = ed[i].next) {
    int v = ed[i].v;
    if (v == fa || v == son[u]) continue;
    dfs2(v, u, false);
  }
  if (son[u]) {
    dfs2(son[u], u, 1);
    flag = son[u];
  }
  count(u, fa, 1);
  flag = 0;
  ans[u] = sum;
  if (!keep) {
    count(u, fa, -1);
    sum = maxc = 0;
  }
}
int main() {
  cin >> n;
  for (int i = 1; i < n + 1; i++) scanf("%d", &col[i]);
  memset(head, -1, sizeof head);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  dfs(1, 0);
  dfs2(1, 0, 0);
  for (int i = 1; i < n + 1; i++) printf("%lld ", ans[i]);
}
