#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, a, b, cnt = 0;
vector<int> color(N), pos(N);
vector<long long> ans(N);
vector<map<long long, long long> > sets(N), Cnt(N);
vector<vector<int> > adj(N);
void dfs(int par, int u) {
  int children = 0, mx = 0, ch;
  for (int v : adj[u]) {
    if (v != par) {
      ++children;
      dfs(u, v);
      if (sets[pos[v]].size() > mx) {
        mx = sets[pos[v]].size();
        ch = v;
      }
    }
  }
  if (!children) {
    ++cnt;
    ++sets[cnt][color[u]];
    Cnt[cnt][sets[cnt][color[u]]] += color[u];
    pos[u] = cnt;
  } else {
    for (int v : adj[u]) {
      if (v != par && v != ch) {
        for (auto& p : sets[pos[v]]) {
          sets[pos[ch]][p.first] += p.second;
          Cnt[pos[ch]][sets[pos[ch]][p.first]] += p.first;
        }
        sets[pos[v]].clear();
        Cnt[pos[v]].clear();
      }
    }
    ++sets[pos[ch]][color[u]];
    Cnt[pos[ch]][sets[pos[ch]][color[u]]] += color[u];
    pos[u] = pos[ch];
  }
  ans[u] = Cnt[pos[u]].rbegin()->second;
}
int main(void) {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> color[i];
  }
  for (int i = 1; i <= n - 1; ++i) {
    cin >> a >> b;
    adj[a].emplace_back(b);
    adj[b].emplace_back(a);
  }
  dfs(0, 1);
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << " \n"[i == n];
  }
  return 0;
}
