#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1e6 + 9;
ll a[N];
vector<ll>G[N];
ll sz[N], Son[N];
ll Max= -1;
ll co;
ll sum = 0;
ll cnt[N];
ll Ans[N];
void dfs1(ll u, ll fa) {
ll cmp = -1;
sz[u]++;
for (auto v:G[u]) {
if (v == fa) continue;
dfs1(v,u);
sz[u] += sz[v];
if (sz[v] > cmp) {
cmp = sz[v];
Son[u] = v;
}
}
}
void init(ll u, ll fa) {
cnt[a[u]]--;
for (auto v:G[u]) {
if (v == fa)continue;
//if (v == Son[u])continue;
init(v, u);
}
}
void DFS(ll u, ll fa, ll Pson) {
cnt[a[u]]++;
if (cnt[a[u]] > Max) {
Max = cnt[a[u]];
co = a[u];
} else if (cnt[a[u]] == Max) {
co += a[u];
}
for (auto v:G[u]){
if (v== fa||v==Pson)continue;
DFS(v, u, Pson);
}
}
void dfs2(ll u, ll fa) {
////cout << u << " ";
for (auto v:G[u]) {
if (v == fa || v == Son[u])continue;
dfs2(v,u);
init(v, u);
sum = Max = 0;
}
if (Son[u])dfs2(Son[u], u);
DFS(u, fa, Son[u]);
Ans[u] = co;
}
void solve() {
ll n;cin >> n;
for (ll i = 1; i <= n; i ++) cin >> a[i];
for (ll i = 1; i < n; i ++) {
ll u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dfs1(1, -1);
// for (ll i = 1; i <= n; i ++) {
// cout << i <<"->"<<Son[i] << endl;
// }
dfs2(1, -1);
for (ll i = 1; i <= n; i ++){
cout << Ans[i] << endl;
}
//cout << sum << endl;
}
signed main() {
ll t = 1;//cin >> t;
while (t--) {
solve();
}
}