#include <bits/stdc++.h>

#define NS (100005)

using namespace std;

typedef long long LL;

template<typename _Tp> inline void IN(_Tp &dig)
{
char c; bool flag = 0; dig = 0;
while (c = getchar(), !isdigit(c)) if (c == '-') flag = 1;
while (isdigit(c)) dig = dig * 10 + c - '0', c = getchar();
if (flag) dig = -dig;
}

int n, v[NS], sz[NS], hs[NS];

struct graph
{
int head[NS], nxt[NS << 1], to[NS << 1], sz;
void push(int a, int b)
{
}
} g;

void dfs(int a, int fa)
{
sz[a] = 1;
for (int i = g.head[a]; ~i; i = g.nxt[i])
if (g[i] != fa)
{
dfs(g[i], a), sz[a] += sz[g[i]];
if (sz[g[i]] > sz[hs[a]]) hs[a] = g[i];
}
}

int h, cnt[NS], mx;

LL sum, ans[NS];

void push(int a, int fa, int x)
{
cnt[v[a]] += x;
if (cnt[v[a]] > mx) mx = cnt[v[a]], sum = v[a];
else if (cnt[v[a]] == mx) sum += v[a];
for (int i = g.head[a]; ~i; i = g.nxt[i])
if (g[i] != fa && g[i] != h)
push(g[i], a, x);
}

void dsu(int a, int fa, bool t)
{
for (int i = g.head[a]; ~i; i = g.nxt[i])
if (g[i] != fa && g[i] != hs[a])
dsu(g[i], a, 0);
if (hs[a]) dsu(hs[a], a, 1), h = hs[a];
push(a, fa, 1), ans[a] = sum, h = 0;
if (!t) push(a, fa, -1), mx = 0;
}

int main(int argc, char const* argv[])
{
IN(n);
for (int i = 1; i <= n; i += 1) IN(v[i]);
for (int i = 1, a, b; i < n; i += 1)
IN(a), IN(b), g.push(a, b), g.push(b, a);
dfs(1, 0), dsu(1, 0, 1), printf("%lld", ans[1]);
for (int i = 2; i <= n; i += 1) printf(" %lld", ans[i]);
putchar(10);
return 0;
}


#### Remmina

No puzzle that couldn't be solved.