#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, u, v, col[maxn];
struct Edge {
  int to, nex;
} edge[maxn << 1];
int head[maxn], ecnt;
void add_edge(int u, int v) {
  edge[++ecnt] = {v, head[u]};
  head[u] = ecnt;
}
int hson[maxn], siz[maxn], cnt[maxn], maxx;
long long ans[maxn], sum;
void get_hson(int now, int fa) {
  siz[now] = 1;
  for (int i = head[now]; i; i = edge[i].nex) {
    int to = edge[i].to;
    if (to == fa) continue;
    get_hson(to, now);
    siz[now] += siz[to];
    if (siz[to] > siz[hson[now]]) hson[now] = to;
  }
}
int start;
void go(int now, int fa, int val) {
  cnt[col[now]] += val;
  if (cnt[col[now]] > maxx) {
    sum = col[now];
    maxx = cnt[col[now]];
  } else if (cnt[col[now]] == maxx)
    sum += 1LL * col[now];
  for (int i = head[now]; i; i = edge[i].nex) {
    int to = edge[i].to;
    if (to == fa || to == start) continue;
    go(to, now, val);
  }
}
void dfs(int now, int fa, int flag) {
  for (int i = head[now]; i; i = edge[i].nex) {
    int to = edge[i].to;
    if (to == fa || to == hson[now]) continue;
    dfs(to, now, -1);
  }
  if (hson[now]) {
    dfs(hson[now], now, 1);
    start = hson[now];
  }
  go(now, fa, 1);
  start = 0;
  ans[now] = sum;
  if (flag == -1) {
    go(now, fa, -1);
    maxx = 0;
    sum = 0LL;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", col + i);
  for (int i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  get_hson(1, 0);
  dfs(1, 0, 1);
  for (int i = 1; i <= n; i++) printf("%lld ", ans[i]);
  return 0;
}
