#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1061109567;
const long long LINF = 4557430888798830399ll;
const int MOD = 998244353;
int Col[100005];
vector<int> G[100005];
map<int, int> Map[100005];
int Cnt[100005];
set<int> Set[100005];
long long Sum[100005];
void InsertColor(int id, int col, int cnt) {
  Map[id][col] += cnt;
  int newcnt = Map[id][col];
  if (newcnt < Cnt[id]) return;
  if (newcnt > Cnt[id]) {
    Cnt[id] = newcnt;
    Sum[id] = col;
    return;
  }
  Sum[id] += col;
  return;
}
void dfs(int u, int p) {
  int col = Col[u];
  if (G[u].size() == 1 && p != -1) {
    Map[u][col] = 1;
    Cnt[u] = 1;
    Sum[u] = col;
    return;
  }
  int MaxSizMap = 0, MaxSizMapV = -1;
  for (auto &v : G[u]) {
    if (v == p) continue;
    dfs(v, u);
    if ((int)Map[v].size() > MaxSizMap) {
      MaxSizMap = Map[v].size();
      MaxSizMapV = v;
    }
  }
  swap(Map[u], Map[MaxSizMapV]);
  Cnt[u] = Cnt[MaxSizMapV];
  Sum[u] = Sum[MaxSizMapV];
  InsertColor(u, col, 1);
  for (auto &v : G[u]) {
    if (v == p || v == MaxSizMapV) continue;
    for (auto &it : Map[v]) InsertColor(u, it.first, it.second);
  }
  return;
}
void test_case() {
  int n;
  scanf("%d", &n);
  if (n == 1) {
    int c;
    scanf("%d", &c);
    printf("%d\n", c);
    return;
  }
  for (int i = 1; i <= n; ++i) scanf("%d", &Col[i]);
  for (int i = 1; i <= n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  dfs(1, -1);
  for (int i = 1; i <= n; ++i) printf("%lld%c", Sum[i], " \n"[i == n]);
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; ++i) {
    test_case();
  }
  return 0;
}
