#include <bits/stdc++.h>
using namespace std;
void task();
int main() {
  srand(time(0));
  task();
  return 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
const int M = 1e5 + 10;
map<int, long long int> cnt_sum[N];
map<int, int> col_cnt[N];
long long int ans[N];
vector<int> g[N];
int n;
int col[N];
void _doit(map<int, int>& m1, map<int, long long int>& q1, map<int, int>& m2,
           map<int, long long int>& q2) {
  for (const pair<int, int>& p : m2) {
    int& cur = m1[p.first];
    q1[cur] -= p.first;
    if (q1[cur] == 0) q1.erase(cur);
    cur += p.second;
    q1[cur] += p.first;
  }
}
void doit(map<int, int>& m1, map<int, long long int>& q1, map<int, int>& m2,
          map<int, long long int>& q2) {
  if (m1.size() > m2.size()) {
    _doit(m1, q1, m2, q2);
  } else {
    _doit(m2, q2, m1, q1);
    m1 = move(m2);
    q1 = move(q2);
  }
}
void dfs(int v, int par) {
  int cnt = 0;
  for (int to : g[v])
    if (par != to) {
      dfs(to, v);
      ++cnt;
      doit(col_cnt[v], cnt_sum[v], col_cnt[to], cnt_sum[to]);
      col_cnt[to].clear();
      cnt_sum[to].clear();
    }
  if (cnt == 0) {
    col_cnt[v][col[v]] = 1;
    cnt_sum[v][1] = col[v];
    ans[v] = col[v];
    return;
  }
  {
    pair<int, int> p(col[v], 1);
    int& cur = col_cnt[v][p.first];
    cnt_sum[v][cur] -= p.first;
    if (cnt_sum[v][cur] == 0) cnt_sum[v].erase(cur);
    cur += p.second;
    cnt_sum[v][cur] += p.first;
  }
  ans[v] = cnt_sum[v].rbegin()->second;
}
void task() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d", &col[i]);
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    --a, --b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  dfs(0, -1);
  for (int i = 0; i < n; ++i) printf("%I64d ", ans[i]);
}
