#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
map<int, int> m[N];
set<pair<int, int> > s[N];
vector<int> g[N];
int n, c[N];
long long ans[N];
void dfs(int u, int p) {
  m[u][c[u]] = 1;
  s[u].insert(pair<int, int>(1, c[u]));
  ans[u] = c[u];
  for (int v : g[u]) {
    if (v == p) continue;
    dfs(v, u);
    if (m[u].size() < m[v].size()) {
      m[u].swap(m[v]);
      s[u].swap(s[v]);
      ans[u] = ans[v];
    }
  }
  for (int v : g[u]) {
    if (v == p) continue;
    for (auto x : m[v]) {
      s[u].erase(pair<int, int>(m[u][x.first], x.first));
      m[u][x.first] += x.second;
      if (s[u].empty() || m[u][x.first] > (*--s[u].end()).first)
        ans[u] = x.first;
      else if (m[u][x.first] == (*--s[u].end()).first)
        ans[u] += x.first;
      s[u].insert(pair<int, int>(m[u][x.first], x.first));
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> c[i];
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i) cout << ans[i] << " ";
}
