#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 5;
int n, col[mxn], sz[mxn], cnt[mxn];
vector<int> adj[mxn];
long long ans[mxn], can, maxx;
bool bigboy[mxn];
void szcount(int v = 0, int p = -1) {
  sz[v] = 1;
  for (int u : adj[v])
    if (u != p) szcount(u, v), sz[v] += sz[u];
}
void reval(int x) {
  if (maxx < ++cnt[x])
    maxx = cnt[x], can = x;
  else if (maxx == cnt[x])
    can += x;
}
void add(int u, int p) {
  reval(col[u]);
  for (int v : adj[u])
    if (v != p && bigboy[v] == 0) add(v, u);
}
void rem(int u, int p) {
  cnt[col[u]]--;
  for (int v : adj[u])
    if (v != p && bigboy[v] == 0) rem(v, u);
}
void dfs(int u = 0, int p = -1, bool keep = 1) {
  int mx = 0, big = -1;
  for (int v : adj[u])
    if (v != p && sz[v] > mx) mx = sz[v], big = v;
  for (int v : adj[u])
    if (v != p && v != big) dfs(v, u, 0);
  if (big + 1) dfs(big, u), bigboy[big] = 1;
  add(u, p);
  if (big + 1) bigboy[big] = 0;
  ans[u] = can;
  if (!keep) rem(u, p), maxx = can = 0;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> col[i];
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  szcount();
  dfs();
  for (int i = 0; i < n; i++) cout << ans[i] << ' ';
  cout << endl;
}
