#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e5 + 9;
long long n, c[inf], sz[inf], MaxFrequency[inf], ans[inf];
vector<long long> adj[inf];
map<long long, long long> *Cnt[inf], *SumCnt[inf];
void dfs(long long u, long long p) {
  auto add = [&](long long color, long long val) {
    (*SumCnt[u])[(*Cnt[u])[color]] -= color;
    (*Cnt[u])[color] += val;
    (*SumCnt[u])[(*Cnt[u])[color]] += color;
    MaxFrequency[u] = max(MaxFrequency[u], (*Cnt[u])[color]);
  };
  sz[u] = 1;
  long long mx = 0, heavy = -1;
  for (auto v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
    sz[u] += sz[v];
    if (sz[v] > mx) mx = sz[v], heavy = v;
  }
  if (heavy == -1)
    Cnt[u] = new map<long long, long long>(),
    SumCnt[u] = new map<long long, long long>(), MaxFrequency[u] = 0;
  else
    Cnt[u] = Cnt[heavy], SumCnt[u] = SumCnt[heavy],
    MaxFrequency[u] = MaxFrequency[heavy];
  add(c[u], 1);
  for (auto v : adj[u]) {
    if (v == p || v == heavy) continue;
    for (auto o : *Cnt[v]) add(o.first, o.second);
  }
  ans[u] = (*SumCnt[u])[MaxFrequency[u]];
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> c[i];
  for (long long i = 1; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(1, 0);
  for (long long i = 1; i <= n; i++) cout << ans[i] << " ";
  cout << endl;
}
