#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
vector<int> vec[maxn];
int son[maxn], sz[maxn];
int col[maxn];
int cnt[maxn];
long long ans[maxn];
int maxc;
long long sum;
int n;
void dfs1(int u, int fa) {
  sz[u] = 1;
  for (auto& v : vec[u]) {
    if (v == fa) continue;
    dfs1(v, u);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
  return;
}
void calc(int u, int fa, int tp, 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 (auto& v : vec[u]) {
    if (v == fa || v == tp) continue;
    calc(v, u, tp, val);
  }
  return;
}
void dfs2(int u, int fa, int del) {
  for (auto& v : vec[u]) {
    if (v == fa || v == son[u]) continue;
    dfs2(v, u, 1);
  }
  if (son[u]) dfs2(son[u], u, 0);
  calc(u, fa, son[u], 1);
  ans[u] = sum;
  if (del) {
    calc(u, fa, 0, -1);
    sum = maxc = 0;
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> col[i];
  }
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    vec[u].emplace_back(v);
    vec[v].emplace_back(u);
  }
  dfs1(1, 0);
  dfs2(1, 0, 1);
  for (int i = 1; i <= n; i++) {
    cout << ans[i];
    if (i == n)
      cout << endl;
    else
      cout << ' ';
  }
  return 0;
}
