#include <bits/stdc++.h>
using namespace std;
const int N = 100005, M = 100005 * 2, INF = 0x3f3f3f3f;
inline int read() {
  int out = 0, flag = 1;
  char c = getchar();
  while (c < 48 || c > 57) {
    if (c == '-') flag = 0;
    c = getchar();
  }
  while (c >= 48 && c <= 57) {
    out = (out << 1) + (out << 3) + c - 48;
    c = getchar();
  }
  return flag ? out : -out;
}
long long int sumc, ans[N];
int n, cnt[N], c[N], vis[N], maxcnt;
int sz[N], hson[N], fa[N];
int h[N];
struct EDGE {
  int to, nxt;
} ed[M];
int ne = 1, head[N], from[M], to[M], len[M], nxt[M];
void init_edges() { memset(head, 0, sizeof(head)), ne = 0; }
void add_edge(int u, int v, int l) {
  ne++;
  from[ne] = u;
  to[ne] = v;
  len[ne] = l;
  nxt[ne] = head[u];
  head[u] = ne;
}
void add_biedge(int u, int v, int l) {
  add_edge(u, v, l);
  add_edge(v, u, l);
}
inline void build(int u, int v) {
  ed[++ne] = (EDGE){v, h[u]};
  h[u] = ne;
  ed[++ne] = (EDGE){u, h[v]};
  h[v] = ne;
}
void hld(int u) {
  sz[u] = 1;
  for (int k = head[u]; k; k = nxt[k])
    if (to[k] != fa[u]) {
      fa[to[k]] = u, hld(to[k]), sz[u] += sz[to[k]];
      if (!hson[u] || sz[to[k]] > sz[hson[u]]) hson[u] = to[k];
    }
}
void merge(int u, int add) {
  cnt[c[u]] += add;
  if (add > 0 && cnt[c[u]] >= maxcnt) {
    if (cnt[c[u]] > maxcnt) maxcnt = cnt[c[u]], sumc = 0;
    sumc += c[u];
  }
  for (int k = head[u]; k; k = nxt[k])
    if (!vis[to[k]] && (to[k] != fa[u])) merge(to[k], add);
}
void dfs2(int u, bool keep) {
  for (int k = head[u]; k; k = nxt[k])
    if (to[k] != fa[u] && to[k] != hson[u]) dfs2(to[k], false);
  if (hson[u]) dfs2(hson[u], true), vis[hson[u]] = true;
  merge(u, 1), ans[u] = sumc;
  if (hson[u]) vis[hson[u]] = false;
  if (!keep) merge(u, -1), sumc = maxcnt = 0;
}
int main() {
  n = read();
  for (int i = 1; i <= (n); i++) c[i] = read();
  for (int i = 1; i < n; i++) add_biedge(read(), read(), 1);
  hld(1);
  dfs2(1, false);
  for (int i = 1; i <= (n); i++) printf("%I64d ", ans[i]);
  return 0;
}
