#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
vector<long long int> ans(100002), mx(100002), col(100002), vec[100002];
map<int, int> mp[100002];
void solve(int node, int ata) {
  for (auto it : vec[node]) {
    if (it != ata) {
      solve(it, node);
      if (mp[it].size() > mp[node].size()) {
        swap(mp[it], mp[node]);
        ans[node] = ans[it];
        mx[node] = mx[it];
      }
      for (auto p : mp[it]) {
        mp[node][p.first] += p.second;
        if (mp[node][p.first] == mx[node]) {
          ans[node] += p.first;
        } else if (mp[node][p.first] > mx[node]) {
          mx[node] = mp[node][p.first];
          ans[node] = p.first;
        }
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; n >= i; i++) {
    cin >> col[i];
    mp[i][col[i]] = 1;
    ans[i] = col[i];
    mx[i] = 1;
  }
  for (int i = 1; n > i; i++) {
    int a, b;
    cin >> a >> b;
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  solve(1, 0);
  for (int i = 1; n >= i; i++) {
    cout << ans[i] << " ";
  }
}
