#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, color[N];
long long ans[N];
vector<int> tree[N];
map<int, long long> freq[N], sum[N];
void merge(int u, int v) {
  if (freq[u].size() < freq[v].size())
    swap(freq[u], freq[v]), swap(sum[u], sum[v]);
  for (auto it = freq[v].begin(); it != freq[v].end(); it++) {
    int c = it->first, f = it->second;
    sum[u][freq[u][c]] -= c;
    freq[u][c] += f;
    sum[u][freq[u][c]] += c;
  }
}
void dfs(int u, int parent) {
  freq[u][color[u]] = 1;
  sum[u][1] = color[u];
  for (int i = 0; i < ((int)tree[u].size()); i++) {
    int v = tree[u][i];
    if (parent == v) continue;
    dfs(v, u);
    merge(u, v);
  }
  ans[u] = sum[u].rbegin()->second;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", color + i);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--, y--;
    tree[x].push_back(y);
    tree[y].push_back(x);
  }
  dfs(0, -1);
  for (int i = 0; i < n; i++)
    printf("%lld%s", ans[i], (i == n - 1) ? "\n" : " ");
  return 0;
}
