#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct Edge {
  int to, nxt;
} edge[maxn << 1];
int head[maxn], siz[maxn], son[maxn], color[maxn], cnt[maxn];
long long sum[maxn], ans;
int n, tot, Son, maxcnt;
inline void addedge(int u, int v) {
  edge[++tot].to = v, edge[tot].nxt = head[u], head[u] = tot;
}
void dfs1(int u, int f) {
  siz[u] = 1;
  int v;
  for (int i = head[u]; i; i = edge[i].nxt) {
    v = edge[i].to;
    if (v == f) continue;
    dfs1(v, u);
    siz[u] += siz[v];
    if (siz[v] > siz[son[u]]) son[u] = v;
  }
}
inline void modify(int u, int f, int val) {
  cnt[color[u]] += val;
  if (cnt[color[u]] > maxcnt)
    maxcnt = cnt[color[u]], ans = color[u];
  else if (cnt[color[u]] == maxcnt)
    ans += color[u];
  int v;
  for (int i = head[u]; i; i = edge[i].nxt) {
    v = edge[i].to;
    if (v == f || v == Son) continue;
    modify(v, u, val);
  }
}
void dfs2(int u, int f, bool tag) {
  int v;
  for (int i = head[u]; i; i = edge[i].nxt) {
    v = edge[i].to;
    if (v == f || v == son[u]) continue;
    dfs2(v, u, 0);
  }
  if (son[u]) dfs2(son[u], u, 1), Son = son[u];
  modify(u, f, 1);
  sum[u] = ans;
  Son = 0;
  if (!tag) {
    modify(u, f, -1);
    maxcnt = ans = 0;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &color[i]);
  int u, v;
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    addedge(u, v), addedge(v, u);
  }
  dfs1(1, 0);
  dfs2(1, 0, 0);
  for (int i = 1; i <= n; i++) printf("%lld ", sum[i]);
  return 0;
}
