n = int(raw_input())
ex = range(n)
ar = map(int,raw_input().split())
flag = False

if ar[0]>=ex[0]:
    dif = ar[0]-ex[0]
else:
    dif = ar[0]-ex[0] + n

if ar[1]<=ex[1]:
    dif1 = ex[1]-ar[1]
else:
    dif1 = ex[1]-ar[1] + n

if dif==dif1:
    flag = True

if flag==True:
    for i in range(2,n):
        if i%2==0:
            if ar[i]>=ex[i]:
                dif = ar[i]-ex[i]
            else:
                dif = ar[i]-ex[i] + n
            if dif != dif1:
                flag = False
                break

        if i%2==1:
            if ar[i]<=ex[i]:
                dif = ex[i]-ar[i]
            else:
                dif = ex[i]-ar[i] + n
            if dif != dif1:
                flag = False
                break

if flag == True:
    print 'YES'
else:
    print 'NO'


n = int(raw_input())
ex = range(n)
ar = map(int,raw_input().split())
flag = True

if ar[0]>=ex[0]:
    dif1 = ar[0]-ex[0]
else:
    dif1 = ar[0]-ex[0] + n

for i in range(2,n,2):
    if ar[i]>=ex[i]:
        dif = ar[i]-ex[i]
    else:
        dif = ar[i]-ex[i] + n
    if dif != dif1:
        flag = False
        break
for i in range(1,n,2):
    if ar[i]<=ex[i]:
        dif = ex[i]-ar[i]
    else:
        dif = ex[i]-ar[i] + n
    if dif != dif1:
        flag = False
        break

if flag == True:
    print 'YES'
else:
    print 'NO'


# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

n = int(raw_input())
a = map(int, raw_input().split())
goal = range(n)

for i in xrange(n):
    for j in xrange(n):
        if j % 2 == 0:
            a[j] = (a[j] + 1) % n
        else:
            a[j] = (a[j] - 1 + n) % n
    if a == goal:
        print "Yes"
        break
else:
    print "No"


t=int(raw_input())
f=[int(x) for x in raw_input().split()]
def change(a,x):
    for y in range(len(a)):
        if (y+1)%2==0:
            d=a[y]-x
            if d<0:
                a[y]=len(a)+d
            else:
                a[y]=d
        else:
            a[y]=(a[y]+x)%len(a)
    return a
d=f[0]-0
d=len(f)-d

change(f,d)
c=0
for x in range(len(f)):
    if f[x]==x:
        c+=1
if c==(len(f)):
    print "Yes"
else:
    print "No"


n = input()
a = list(map(int, raw_input().split()))
ok = 0
for i in range(n):
    for j in range(n):
        if j % 2 == 0:
            a[j] = (a[j] - 1 + n) % n
        else:
            a[j] = (a[j] + 1) % n
    if a == range(n):
        ok = 1
print "Yes" if ok else "No"


n=int(raw_input())
a=map(int,raw_input().split())
delta=n-a[0]
for i in xrange(n):
	tmp=0
	if i&1: tmp=a[i]-delta
	else: tmp=a[i]+delta
	tmp=(tmp%n+n)%n
	if tmp!=i:
		print 'No'
		exit()
print 'Yes'


t=input()
n=map(int,raw_input().split())
flag=0
d1=t-n[0]
for i in xrange(1,t):
    if(i%2==1):
        if((n[i]+t-d1)%t!=i):
            print "NO"
            flag=1
            break
    else:
        if((n[i]+d1)%t!=i):
            print "NO"
            flag=1
            break
if(flag==0):
    print "YES"


import copy
a=int(raw_input())
c=map(int,raw_input().split())
z=[i for i in range(a)]
s=a-c[0]
for i in range(a):
    if (i%2)==0:
        c[i]=(c[i]+s)%(a)
    else:
        if (c[i]-s)<0:
            c[i]=(a+c[i]-s)%a
        else:
            c[i]=c[i]-s
if z==c:
    print "Yes"
else:print "No"


# your code goes here
import sys
n=int(raw_input())
a=map(int,sys.stdin.readline().split(" "))
val=[0]*(n+1)
for i in range(0,n):
    if(i%2==1):
        if(i-a[i]>0):
            val[i]=n-(i-a[i])
        else:
            val[i]=a[i]-i
    else:
        if(i-a[i]<0):
            val[i]=n-(a[i]-i)
        else:
            val[i]=i-a[i]

flag=True
for i in range(0,n-1):
    if(val[i]!=val[i+1]):
        flag=False
        break
if(flag):
    print "Yes"
else:
    print "No"


import sys

N = input()
S = map(int, raw_input().split())

dif1 = 0
dif2 = 0

if ( N == 1 ):
    if ( S[0] == 0 ):
        print("Yes")
    else:
        print("No")
else:
    for i in range(N):

        if (i%2 == 0):
            if(S[i] > i):
                dif2 = N-S[i]+i
            else:
                dif2 = i-S[i]
        else:
            if(S[i] < i):
                dif2 = N+S[i]-i
            else:
                dif2 = S[i]-i

        if (i == 0):
            dif1 = dif2
        else:
            if (dif1 != dif2):
                print("No")
                sys.exit()
            else:
                dif1 = dif2

print ("Yes")


import sys

N = input()
S = map(int, raw_input().split())

dif1 = 0
dif2 = 0
flag = 1

if ( N == 1 ):
    if ( S[0] != 0 ):
        flag = 0
else:
    for i in range(N):

        if (i%2 == 0):
            if(S[i] > i):
                dif2 = N-S[i]+i
            else:
                dif2 = i-S[i]
        else:
            if(S[i] < i):
                dif2 = N+S[i]-i
            else:
                dif2 = S[i]-i

        if (i == 0):
            dif1 = dif2
        else:
            if (dif1 != dif2):
                flag = 0
                break
            else:
                dif1 = dif2

if(flag):
    print("Yes")
else:
    print("No")


#coding: utf-8

def main():
    n = int(raw_input())
    a = map(int, raw_input().split())
    for i in range(n):
        if a == range(n):
            break
        a = map(lambda tup: (tup[1]+1)%n if (tup[0]&1) else (tup[1]-1+n)%n, enumerate(a))
    else:
        print "No"
        return
    print "Yes"
if __name__ == "__main__":
    main()


n = input()
st = raw_input()
l = st.split()
inputlist = []
for i in range(n):
	inputlist.append(int(l[i]))
l = [i for i in range(n)]
check = 0
for i in range(n):
	for i in range(n):
		if (i%2==0):
			if(inputlist[i]==n-1):
				inputlist[i]=0
			else:
				inputlist[i]+=1
		else:
			if(inputlist[i]==0):
				inputlist[i] = n-1
			else:
				inputlist[i]-=1

	if inputlist==l:
		print "Yes"
		check = 1
if check == 0:
	print "No"


l,a=input(),map(int, raw_input().split())
t=a[0]
for i,c in enumerate(a):
    a[i] = (c+t if i%2 else c-t)%l
print "Yes" if a == range(l) else "No"


l,a=range(input()),map(int,raw_input().split())
t=a[0]
for i in l:
    a[i]=(a[i]-t*(-1)**i)%(l[-1]+1)
print "Yes" if a==l else "No"


l,a=input(),map(int,raw_input().split())
t=a[0]
for i in range(l):
    a[i]=(a[i]-t*(-1)**i)%l
print "Yes" if a==range(l) else "No"


n=input()
o="Yes"
a=map(int,raw_input().split())
for i in range(1,len(a)):
    tmp=a[i]+a[0]
    if (tmp>n-1):
        tmp-=n
    if (tmp<0):
        tmp=n+tmp
    if (tmp!=i):
        o="No"
    a[0]*=-1
print o


l=lambda:map(int,raw_input().split())
n=input()
a=l()
x=n-a[0]
print(['No','Yes'][[(a[i]+((-1)**i)*x)%n for i in range(n)]==range(n)])


def progress(N,A):
	for i in xrange(N):
		if(i%2==0):
			A[i]=(A[i]+1)%N
		else:
			A[i]=(A[i]-1+N)%N

def check(N,A):
	for i in xrange(N):
		if(A[i]!=i):
			return False
	return True

N = int(raw_input())
A = list(int(i) for i in  tuple(raw_input().split()))
b = False
for i in xrange(N):
	b = b or check(N,A)
	progress(N,A)
if(b):
	print ("Yes")
else:
	print("No")


n = input()
seq = map(int, raw_input().split())
dif = n - seq[0]
if dif != 0:
    seq = [(s + (-1)**i * dif)%n for i, s in enumerate(seq)]
print "Yes" if seq == range(n) else "No"


n = input()
a = map(int, raw_input().split())
for i in xrange(n):
    a = [(a[j]+(j%2)-(j%2==0))%n for j in xrange(n)]
    if a == range(n):
        print "Yes"
        quit()
print "No"


__author__ = 'Nate'

def simulate(active_teeth, n):
    goal = ''.join([str(i) for i in range(n)])

    for i in range(n):
        res = ''
        for j in range(n):
            if j % 2 == 0:
                me = active_teeth[j] + 1
                me %= (n)
                active_teeth[j] = me
                res += str(me)
            else:
                me = active_teeth[j] - 1
                me %= (n)
                active_teeth[j] = me
                res += str(me)
        #print res
        if res == goal:
            return 'Yes'
    return 'No'

n = input()
active_teeth = map(int, raw_input().split())
print simulate(active_teeth, n)


n = input()
act = [int(i) for i in raw_input().split()]
req = range(n)
xy=0
for i in xrange(n+1000):
    if(act==req):
        print "Yes"
        xy=1
        break
    for j in xrange(n):
        if(j%2==0):
            act[j]=(act[j]+1)%n
        else:
            act[j]=(act[j]+n-1)%n
if(xy==0):
    print "No"


n = input()
act = [int(i) for i in raw_input().split()]
req = range(n)
xy=0
for i in xrange(n):
    if(act==req):
        print "Yes"
        xy=1
        break
    for j in xrange(n+1000):
        if(j%2==0):
            act[j]=(act[j]+1)%n
        else:
            act[j]=(act[j]+n-1)%n
if(xy==0):
    print "No"


n = int(raw_input())
S = map(int, raw_input().split(" "))
rots = n - S[0]
print S
for i in xrange(n):
    S[i] = (S[i] + ((-1) ** i) * rots) % n
if S == range(n):
    print "Yes"
else:
    print "No"


n = int(raw_input())
S = map(int, raw_input().split(" "))
rots = n - S[0]
for i in xrange(n):
    S[i] = (S[i] + ((-1) ** i) * rots) % n
if S == range(n):
    print "Yes"
else:
    print "No"


n = input()
lis= map(int, raw_input().split())
satis1 = 0
satis2 = 0
if(len(lis)==1):
    print 'Yes'
else:
    dis1 = (lis[0]-0)%n
    dis2 = (lis[1]-1)%n
    if(dis1 != (-dis2)%n):
        print 'No'
    else:
        i=0
        while(i<n):
            if(dis1 != (lis[i]-i)%n):
                break
            i +=2
        if (i>=n):
            satis1 = 1
        i=1
        while(i<n):
            if(dis2 != (lis[i]-i)%n):
                break
            i +=2
        if(i>=n):
            satis2 = 1
        if((satis1 ==1) & (satis2 ==1)):
            print 'Yes'
        else:
            print 'No'


n=input()
a=map(int,raw_input().split())
print "Yes" if all(i==(a[i]+(-1)**i*(n-a[0]))%n for i in range(n)) else "No"


n = int(raw_input())
raw_a = raw_input().split()
a = [int(i) for i in raw_a]
answer = 'No'
for i in range(n):
    for j in range(n):
        if j % 2 == 0:
            a[j] = (a[j]+1) % n
        else:
            a[j] = (a[j]-1) % n
    can = True
    for j in range(n):
        if not a[j] == j:
            can = False
    if can:
        answer = 'Yes'

print answer


#!/usr/bin/python
# Filename = 556B.py

n = int(raw_input())
a = map(int, raw_input().split())
d = a[0]
k = -1
flag = True
for i in range(0, n):
    if (a[i] + k * d + n) % n != i:
        flag = False
        break
    k *= -1
if flag:
    print('Yes')
else:
    print('No')


n=input()
x=raw_input().split(' ')
save=(0-int(x[0]))%n
cnt=0
i=1
while i<n:
    if i%2==0:
        if (i-int(x[i]))%n!=save:
            cnt=1
            break
    else:
        if (n-((i-int(x[i]))%n))%n!=save:
            cnt=1
            break
    i=i+1
if cnt==0:
    print "Yes"
else:
    print "No"


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(int i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const int sz = 100001;
std::vector<int> g[sz];
int t, i, x, j, y, 	z, k, n;
int col[sz],  vis[sz];
ll ans[sz];

map<int, ll> ma[sz], sec[sz];

void dfs(int v, int p) {
	// vis[v] = 1;

	ll  mx = -hell;
	ll &cur = ans[v];
	sec[v][1] += col[v];
	ma[v][col[v]] = 1;

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if ((int)ma[zx].size() > (int)ma[v].size())	{
				swap(ma[zx], ma[v]);
				swap(sec[zx], sec[v]);
			}
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				sec[v][ma[v][i]] -= i;
				ma[v][i] += val;
				sec[v][ma[v][i]] += i;
			}
			// ma[zx].clear();
			// sec[zx].clear();
		}
	}

	cur = sec[v].rbegin()->ss;

	debug(ma[v]);

	// for (auto &vy : ma[v])	{
	// 	ll i = vy.ff, val = vy.ss;
	// 	mx = max(mx, val);
	// }

	// for (auto &vy : ma[v])	{
	// 	ll i = vy.ff, val = vy.ss;
	// 	cur += ( mx == val ) ? i : 0;
	// }

	// ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	// repr(i, n, 0, -1) {
	// 	if (!vis[i])	dfs(i, -1);
	// }
	dfs(1, -1);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100001;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz], vis[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	vis[v] = 1;

	ll cur = 0, mx = 0;

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() > ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				if (val)
					ma[v][i] += val;
			}
			ma[zx].clear();
		}
	}

	// debug(ma[v]);
	ma[v][col[v]] += 1;

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += ( mx == val ) ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	// repr(i, n, 0, -1) {
	// 	if (!vis[i])	dfs(i, -1);
	// }
	dfs(1, -1);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define inf (int)( 1e9 + 10 )
#define mxs (int)( 1e6 + 10 )

ll ans, t, n, m, i, j , k, a, b, c, d, e, f, g, sz, q, mx, cases, pos, l, r;
string s , s2 , s3 , s4;
vector < ll > graph [ mxs ];
bool visited [ mxs ];
ll ar [ mxs ] , start [ mxs ] , finish [ mxs ] , mo [ mxs ] , sum [ mxs ] , counter [ mxs ] , lol [ mxs ];

struct dataxxx {
	ll le , re , id;
};

bool cmp(dataxxx &x, dataxxx &y) {
	ll block_x = x.le / k, block_y = y.le / k;
	if (block_x == block_y) return x.re < y.re;
	return block_x < block_y;
}

dataxxx qu[mxs];

void dfs(ll source) {
	start[source] = ++a;
	mo[a] = source;
	visited[source] = 1;

	for (ll bb : graph[source]) {
		if (!visited[bb]) {
			dfs(bb);
		}
	}

	finish[source] = ++a;
	mo[a] = source;
}

void add(ll idx) {
	a = ar[mo[idx]];

	sum[counter[a]] -= a;
	counter[a]++;
	sum[counter[a]] += a;

	if (counter[a] > pos)	pos = counter[a];
}

void rem(ll idx) {
	a = ar[mo[idx]];
	if (pos == counter[a] && sum[counter[a]] <= a)	pos--;

	sum[counter[a]] -= a;
	counter[a]--;
	sum[counter[a]] += a;
}

void brainfuck() {
	cin >> n;
	k = sqrt( n + 1 );
	for (i = 1 ; i <= n; i++) cin >> ar[i];

	for (i = 1; i < n; i++) {
		cin >> a >> b;
		graph[a].push_back(b);
		graph[b].push_back(a);
	}

	dfs(1);
	for (i = 1; i <= n; i++) {
		qu[i].le = start[i];
		qu[i].re = finish[i];
		qu[i].id = i;
	}
	sort(qu + 1, qu + n + 1, cmp);

	for (int i = 1; i <= n; i++) {
		while (l > qu[i].le)	add(--l);
		while (r < qu[i].re)	add(++r);
		while (l < qu[i].le)	rem(l++);
		while (r > qu[i].re)	rem(r--);
		lol[qu[i].id] = sum[pos];
	}
	for (int i = 1; i <= n; i++)	cout << lol[i] << " ";
}

int main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	brainfuck();
	return 0;
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100002;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	ma[v][col[v]] += 1;
	ll cur = 0, mx = 0;

	for (auto zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() < ma[v].size())	swap(ma[zx], ma[v]);
			for (auto [i, val] : ma[zx])	ma[v][i] += val;
			ma[zx].clear();
		}
	}

	debug(ma[v]);

	for (auto [i, val] : ma[v]) {
		mx = max(mx, val);
	}

	for (auto [i, val] : ma[v]) {
		cur += mx == val ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dfs(1, 0);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100009;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	ma[v][col[v]] += 1;
	ll cur = 0, mx = 0;

	for (auto zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() < ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto [i, val] : ma[zx])	ma[v][i] += val;
		}
	}

	debug(ma[v]);

	for (auto [i, val] : ma[v]) {
		mx = max(mx, val);
	}

	for (auto [i, val] : ma[v]) {
		cur += mx == val ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dfs(1, 0);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100002;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz], vis[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	vis[v] = 1;

	ll cur = 0, mx = 0;

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() > ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				ma[v][i] += val;
			}
			ma[zx].clear();
		}
	}

	// debug(ma[v]);
	ma[v][col[v]] += 1;

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += ( mx == val ) ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	repr(i, n, 0, -1) {
		if (!vis[i])	dfs(i, -1);
	}
	// dfs(1, 0);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(int i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const int sz = 100001;
std::vector<int> g[sz];
int t, i, x, j, y, 	z, k, n;
int col[sz],  vis[sz];
ll ans[sz];

map<int, int> ma[sz];

void dfs(int v, int p) {
	// vis[v] = 1;

	ll  mx = -hell;
	ll &cur = ans[v];

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if ((int)ma[zx].size() > (int)ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				if (val)
					ma[v][i] += val;
				// mx = max(mx, ma[v][i]);
			}
			ma[zx].clear();
		}
	}

	ma[v][col[v]] += 1;

	// debug(ma[v]);

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += ( mx == val ) ? i : 0;
	}

	// ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	// repr(i, n, 0, -1) {
	// 	if (!vis[i])	dfs(i, -1);
	// }
	dfs(1, -1);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const int sz = 100001;
std::vector<int> g[sz];
int t, i, x, j, y, 	z, k, n;
int col[sz],  vis[sz];
int ans[sz];

map<int, int> ma[sz];

void dfs(int v, int p) {
	// vis[v] = 1;

	ll  mx = -hell;
	int &cur = ans[v];

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if ((int)ma[zx].size() > (int)ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				if (val)
					ma[v][i] += val;
				// mx = max(mx, ma[v][i]);
			}
			ma[zx].clear();
		}
	}

	ma[v][col[v]] += 1;

	// debug(ma[v]);

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += ( mx == val ) ? i : 0;
	}

	// ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	// repr(i, n, 0, -1) {
	// 	if (!vis[i])	dfs(i, -1);
	// }
	dfs(1, -1);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100002;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	ma[v][col[v]] += 1;
	ll cur = 0, mx = 0;

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() > ma[v].size())	swap(ma[zx], ma[v]);

		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				ma[v][i] += val;
			}
			ma[zx].clear();
		}
	}

	debug(ma[v]);

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += mx == val ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dfs(1, 0);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const int sz = 100001;
std::vector<int> g[sz];
int t, i, x, j, y, 	z, k, n;
int col[sz],  vis[sz];
int ans[sz];

map<int, int> ma[sz];

void dfs(int v, int p) {
	vis[v] = 1;

	ll  mx = -hell;
	int &cur = ans[v];

	for (auto &zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() > ma[v].size())	swap(ma[zx], ma[v]);
		}
	}

	for (auto zx : g[v]) {
		if (zx != p) {
			for (auto &vy : ma[zx])	{
				ll i = vy.ff, val = vy.ss;
				if (val)
					ma[v][i] += val;
				// mx = max(mx, ma[v][i]);
			}
			ma[zx].clear();
		}
	}

	ma[v][col[v]] += 1;

	// debug(ma[v]);

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		mx = max(mx, val);
	}

	for (auto &vy : ma[v])	{
		ll i = vy.ff, val = vy.ss;
		cur += ( mx == val ) ? i : 0;
	}

	// ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	// repr(i, n, 0, -1) {
	// 	if (!vis[i])	dfs(i, -1);
	// }
	dfs(1, -1);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front priority_queue

#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define debug(...) 42
#endif

#define endl '\n'
#define ll long long
#define rint register int
#define minpq priority_queue <int, vector<int>, greater<int> >
#define maxpq  priority_queue <int>

#define re register
#define pb(x) push_back(x);
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;
#define scan(a, n) 		for(int i = 0; i < n; i++)cin >> a[i];

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007

const ll sz = 100009;
std::vector<ll> g[sz];
ll t, i, x, j, y, 	z, k, n;
ll col[sz], ans[sz];

map<ll, ll> ma[sz];

void dfs(ll v, ll p) {
	ma[v][col[v]] += 1;
	ll cur = 0, mx = 0;

	for (auto zx : g[v]) {
		if (zx != p) {
			dfs(zx, v);
			if (ma[zx].size() > ma[v].size())	swap(ma[zx], ma[v]);
			for (auto [i, val] : ma[zx])	ma[v][i] += val;
			ma[zx].clear();
		}
	}

	debug(ma[v]);

	for (auto [i, val] : ma[v]) {
		mx = max(mx, val);
	}

	for (auto [i, val] : ma[v]) {
		cur += mx == val ? i : 0;
	}

	ans[v] += cur;
}

int32_t main() {
	ios::sync_with_stdio(0); 		cin.tie(0); cout.tie(0);

	cin >> n;

	rep(i, 1, n + 1, 1)	cin >> col[i];

	rep(i, 0, n - 1, 1) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	dfs(1, 0);

	rep(i, 1, n + 1, 1)	cout << ans[i] << " "; cout << "\n";

	// cerr << "ans : "; rep(i, 1, n + 1, 1)	cerr << ans[i] << " "; cerr << "\n";

}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int>* m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x] = new map<int, int>();
    (*m[x])[c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if ((*m[x]).sz > (*m[i]).sz)
                swap(m[x], m[i]);
            for (auto& j : *m[i]) {
                (*m[x])[j.F] += j.S;
            }
            delete m[i];
        }
    }
    for (auto& i : *m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : *m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if (m[x].sz < m[i].sz)
                swap(m[x], m[i]);
            for (auto& j : m[i]) {
                m[x][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];
pair<int, ll> best[N];

int dfs(int x, int p)
{
    int mi = x;
    m[x][c[x]]++;
    best[x] = { 1, c[x] };
    for (auto& i : g[x]) {
        if (i != p) {
            i = dfs(i, x);
            int z;
            if (m[mi].sz < m[i].sz) {
                z = mi;
                mi = i;
            } else {
                z = i;
            }
            for (auto& j : m[z]) {
                m[mi][j.F] += j.S;
                if (m[mi][j.F] > best[mi].F) {
                    best[mi].F = m[mi][j.F];
                    best[mi].S = j.F;
                } else if (m[mi][j.F] == best[mi].F)
                    best[mi].S += j.F;
            }
        }
    }
    ans[x] = best[mi].S;
    return mi;
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int>* m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x] = new map<int, int>();
    (*m[x])[c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if ((*m[x]).sz > (*m[i]).sz)
                swap(m[x], m[i]);
            for (auto& j : *m[i]) {
                (*m[x])[j.F] += j.S;
            }
            (*m[i]).clear();
        }
    }
    for (auto& i : *m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : *m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int>* m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x] = new map<int, int>();
    (*m[x])[c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if ((*m[x]).sz > (*m[i]).sz)
                swap(m[x], m[i]);
            for (auto& j : *m[i]) {
                (*m[x])[j.F] += j.S;
            }
        }
    }
    for (auto& i : *m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : *m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];

int dfs(int x, int p)
{
    int mx = 0, z = 0, mi = x;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            i = dfs(i, x);
            if (m[mi].sz < m[i].sz) {
                z = mi;
                mi = i;
            } else {
                z = i;
            }
            for (auto& j : m[z]) {
                m[mi][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[mi]) {
        smax(mx, i.S);
    }
    for (auto& i : m[mi]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
    return mi;
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            for (auto& j : m[i]) {
                m[x][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if (m[x].sz < m[i].sz)
                swap(m[x], m[i]);
            for (auto& j : m[i]) {
                m[x][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int> m[N];

void dfs(int x, int p)
{
    int mx = 0, z = 0, mi = x;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if (m[mi].sz < m[i].sz) {
                z = mi;
                mi = i;
            } else {
                z = i;
            }
            for (auto& j : m[z]) {
                m[mi][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[mi]) {
        smax(mx, i.S);
    }
    for (auto& i : m[mi]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int>* m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x] = new map<int, int>();
    (*m[x])[c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if ((*m[x]).sz < (*m[i]).sz)
                swap(m[x], m[i]);
            for (auto& j : *m[i]) {
                (*m[x])[j.F] += j.S;
            }
        }
    }
    for (auto& i : *m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : *m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N];
ll ans[N];
vector<int> g[N];
map<int, int>* m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x] = new map<int, int>();
    (*m[x])[c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            if ((*m[x]).sz > (*m[i]).sz)
                swap(m[x], m[i]);
            for (auto& j : *m[i]) {
                (*m[x])[j.F] += j.S;
            }
            m[i] = NULL;
        }
    }
    for (auto& i : *m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : *m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define gcd __gcd
#define F first
#define B begin()
#define E end()
#define ln length()
#define sz size()
#define S second
#define em empty()
#define mp make_pair
#define pb push_back
#define pq priority_queue
#define um unordered_map
#define us unordered_set
#define lb lower_bound
#define ub upper_bound
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define sbits(a) __builtin_popcount(a)
#define lcm(a, b) ((a * b) / gcd(a, b))
#define nani(x) cerr << #x << " is " << x << endl;
#define adde(g, u, v) g[u].pb(v), g[v].pb(u);
#define addew(g, u, v, w) g[u].pb({ v, w }), g[v].pb({ u, w });
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define inf 10000000000
#define mod 1000000007

typedef long long ll;

const int N = 1e5 + 5;

int c[N], ans[N];
vector<int> g[N];
map<int, int> m[N];

void dfs(int x, int p)
{
    int mx = 0;
    m[x][c[x]]++;
    for (auto& i : g[x]) {
        if (i != p) {
            dfs(i, x);
            for (auto& j : m[i]) {
                m[x][j.F] += j.S;
            }
        }
    }
    for (auto& i : m[x]) {
        smax(mx, i.S);
    }
    for (auto& i : m[x]) {
        if (i.S == mx)
            ans[x] += i.F;
    }
}

int main()
{
    fast;

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    int n;
    cin >> n;

    for (int i = 1; i <= n; i++)
        cin >> c[i];

    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adde(g, u, v);
    }

    dfs(1, 0);

    for (int i = 1; i <= n; i++)
        cout << ans[i] << ' ';

    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
        mp[u].clear();
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];
int c[N];
ll ans[N];
int mxt[N];
map<int, int> cl[N];

void dfs(int u = 1, int fa = 0){
    for(auto v: adj[u]){
        if (fa == v) continue;
        dfs(v, u);
        if(cl[v].size() > cl[u].size()){
            swap(cl[v], cl[u]);
            mxt[u] = mxt[v];
            ans[u] = ans[v];
        }
        for(auto it: cl[v]){
            int x = it.first;
            int y = it.second;
            cl[u][x] += y;
            if(cl[u][x] == mxt[u]) ans[u] += x;
            else if(cl[u][x] > mxt[u]){
                ans[u] = x;
                mxt[u] = cl[u][x] ;
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> c[i];
        ans[i] = c[i];
        mxt[i] = 1;
        cl[i][c[i]] = 1;
    }

    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs();
    for(int i = 1; i <=n; ++i) cout << ans[i] << ' ';
    return 0;
}

// do something rather than nothing
// a master has failed more times than a beginner has even tried


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        mp[u].clear();
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
int ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        int p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += (it->first);
            }else break;
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        auto it = mp[i].begin();
        //for(; it != mp[i].end(); ++it){
            //cout << it->first << ' ' << it->second << '\t';
        //}
        //cout << '\n';
        //cout << deg[i] << '\n';
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*S(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
int ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        int p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += (it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        auto it = mp[i].begin();
        //for(; it != mp[i].end(); ++it){
            //cout << it->first << ' ' << it->second << '\t';
        //}
        //cout << '\n';
        //cout << deg[i] << '\n';
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];
int c[N];
int ans[N];
int mxt[N];
map<int, int> cl[N];

void dfs(int u = 1, int fa = 0){
    for(auto v: adj[u]){
        if (fa == v) continue;
        dfs(v, u);
        if(cl[v].size() > cl[u].size()){
            swap(cl[v], cl[u]);
            mxt[u] = mxt[v];
            ans[u] = ans[v];
        }
        for(auto it: cl[v]){
            int x = it.first;
            int y = it.second;
            cl[u][x] += y;
            if(cl[u][x] == mxt[u]) ans[u] += x;
            else if(cl[u][x] > mxt[u]){
                ans[u] = x;
                mxt[u] = cl[u][x] ;
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> c[i];
        ans[i] = c[i];
        mxt[i] = 1;
        cl[i][c[i]] = 1;
    }

    for(int i = 0; i < n - 1; ++i){
        int u, v;
        cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs();
    for(int i = 1; i <=n; ++i) cout << ans[i] << ' ';
    return 0;
}

// do something rather than nothing
// a master has failed more times than a beginner has even tried


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;

        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                if(mp[u].size() > mp[v].size()) swap(mp[u], mp[v]);
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
        mp[u].clear();
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include<bits/stdc++.h>
#define pb push_back

typedef long long ll;
const int N = 1e5+10;

using namespace std;
vector<int> adj[N];

int deg[N];
int c[N];
ll ans[N];
bool vis[N];
map<int, int> mp[N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n; cin >> n;
    for(int i = 1; i <= n; ++i){
        cin >> c[i];
    }
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;

        adj[u].pb(v);
        adj[v].pb(u);

        deg[u]++;
        deg[v]++;
    }
    queue<int> q;
    for(int i = 2; i <= n; ++i){
        if(deg[i]==1){
            q.push(i);
            deg[i]--;
        }
    }
    while(!q.empty()){

        int u = q.front();
        q.pop();
        mp[u][c[u]]++;
        //cout << u << '\n';
        auto it = mp[u].begin();
        int ci = it->second;
        ll p = 0;
        for(; it != mp[u].end(); ++it){
            if(it->second == ci){
                p += 1LL*S(it->first);
            }else if(it->second > ci){
                ci = it->second;
                p = (it->first);
            }
        }
        mp[u].clear();
        ans[u] = p;
        if(u == 1) break;
        for(auto v: adj[u]){
            if(deg[v]){
                auto iter = mp[u].begin();
                for(; iter != mp[u].end(); ++iter){
                    mp[v][(iter->first)] += (iter->second);
                }
                deg[v]--;
            }
            if((deg[v] == 1 && v != 1) || (!deg[v] && v == 1) ){
                q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        cout << ans[i] << ' ';
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

inline void read(int &x)
{
    char ch;
    bool flag = false;
    for(ch = getchar(); !isdigit(ch); ch = getchar())if(ch == '-') flag = true;
    for(x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const int maxn = 1e5 + 5;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_sz(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_sz(v, u);
            sz[u] += sz[u];
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && !son[v])
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int mx = -1, big = -1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && sz[v] > mx)
            mx = sz[v], big = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            dfs(v, u, 0);
    }
    if(big != -1)
    {
        dfs(big, u, 1);
        son[big] = 1;
    }
    add(u, p, 1);
    ans[u] = sum;
    if(big != -1)
        son[big] = 0;
    if(!keep)
    {
        add(u, p, -1);
        maxx = 0;
        sum = 0;
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        read(col[i]);
    int u, v;
    for(int i = 0; i < n - 1; i++)
    {
        read(u);
        read(v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    get_sz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
    {
        if(i != 1)
            printf(" ");
        printf("%lld", ans[i]);
    }
    printf("\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

inline void read(int &x)
{
    char ch;
    bool flag = false;
    for(ch = getchar(); !isdigit(ch); ch = getchar())if(ch == '-') flag = true;
    for(x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const int maxn = 1e5 + 5;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_sz(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_sz(v, u);
            sz[u] += sz[u];
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && !son[v])
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int mx = -1, big = -1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && sz[v] > mx)
            mx = sz[v], big = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            dfs(v, u, 0);
    }
    if(big != -1)
    {
        dfs(big, u, 1);
        son[big] = 1;
    }
    add(u, p, 1);
    if(big != -1)
        son[big] = 0;
    ans[u] = sum;
    if(keep == 0)
    {
        add(u, p, -1);
        maxx = sum = 0;
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        read(col[i]);
    int u, v;
    for(int i = 1; i < n; i++)
    {
        read(u);
        read(v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    get_sz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
        printf("%lld ", ans[i]);
    printf("\n");
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 1e5 + 100;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, big, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_son(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_son(v, u);
            sz[u] += sz[u];
            if(sz[v] > sz[son[u]])
                son[u] = v;
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != son[u])
            dfs(v, u, 0);
    }
    if(son[u])
    {
        dfs(son[u], u, 1);
        big = son[u];
    }
    add(u, p, 1);
    big = 0;
    ans[u] = sum;
    if(!keep)
    {
        add(u, p, -1);
        maxx = 0;
        sum = 0;
    }
}

int main()
{
    while(~scanf("%d", &n))
    {
        big = 0;
        maxx = 0;
        sum = 0;
        for(int i = 1; i <= n; i++)
        {
            G[i].clear();
            sz[i] = son[i] = cnt[i] = ans[i] = 0;
        }
        for(int i = 1; i <= n; i++)
            scanf("%d", &col[i]);
        int u, v;
        for(int i = 0; i < n - 1; i++)
        {
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        get_son(1, 0);
        dfs(1, 0, 1);
        for(int i = 1; i <= n; i++)
        {
            if(i != 1)
                printf(" ");
            printf("%lld", ans[i]);
        }
        printf("\n");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

inline void read(int &x)
{
    char ch;
    bool flag = false;
    for(ch = getchar(); !isdigit(ch); ch = getchar())if(ch == '-') flag = true;
    for(x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const int maxn = 1e5 + 5;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_sz(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_sz(v, u);
            sz[u] += sz[v];
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && !son[v])
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int mx = -1, big = -1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && sz[v] > mx)
            mx = sz[v], big = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            dfs(v, u, 0);
    }
    if(big != -1)
    {
        dfs(big, u, 1);
        son[big] = 1;
    }
    add(u, p, 1);
    if(big != -1)
        son[big] = 0;
    ans[u] = sum;
    if(keep == 0)
    {
        add(u, p, -1);
        maxx = sum = 0;
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        read(col[i]);
    int u, v;
    for(int i = 1; i < n; i++)
    {
        read(u);
        read(v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    get_sz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
        printf("%lld ", ans[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 100005;

int n, Max, cnt[N], sz[N], col[N], big[N];
long long sum, ans[N];
vector<int> g[N];

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > Max)
    {
        sum = col[u];
        Max = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == Max)
    {
        sum += col[u];
    }

    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != p && !big[v])
            add(v, u, x);
    }
}

void dfs(int u, int fa, bool keep)
{
    int mx = -1, bigchild = -1;
    for(auto v : g[u])
        if(v != fa && sz[v] > mx)
            mx = sz[v], bigchild = v;
    for(auto v : g[u])
        if(v != fa && v != bigchild)
            dfs(v, u, 0);
    if(bigchild != -1)
        dfs(bigchild, u, 1), big[bigchild] = 1;
    add(u, fa, 1);
    if(bigchild != -1)
        big[bigchild] = 0;
    ans[u] = sum;
    if(keep == 0)
        add(u, fa, -1), Max = sum = 0;
}

void getsz(int u, int fa)
{
    sz[u] = 1;
    for(auto v : g[u])
        if(v != fa)
        {
            getsz(v, u);
            sz[u] += sz[v];
        }
}
void solve()
{
    cin >> n;
    int u, v;
    for(int i = 1; i <= n; i++) cin >> col[i];
    for(int i = 1; i < n; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
    getsz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
}
int main()
{
    solve();
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

inline void read(int &x)
{
    char ch;
    bool flag = false;
    for(ch = getchar(); !isdigit(ch); ch = getchar())if(ch == '-') flag = true;
    for(x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
    x = flag ? -x : x;
}

const int maxn = 1e5 + 100;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, big, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_son(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_son(v, u);
            sz[u] += sz[u];
            if(sz[v] > sz[son[u]])
                son[u] = v;
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != son[u])
            dfs(v, u, 0);
    }
    if(son[u])
    {
        dfs(son[u], u, 1);
        big = son[u];
    }
    add(u, p, 1);
    big = 0;
    ans[u] = sum;
    if(!keep)
    {
        add(u, p, -1);
        maxx = 0;
        sum = 0;
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        read(col[i]);
    int u, v;
    for(int i = 0; i < n - 1; i++)
    {
        read(u);
        read(v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    get_son(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
    {
        if(i != 1)
            printf(" ");
        printf("%lld", ans[i]);
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 100005;

int n, Max, cnt[N], sz[N], col[N], big[N];
long long sum, ans[N];
vector<int> g[N];

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > Max)
    {
        sum = col[u];
        Max = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == Max)
    {
        sum += col[u];
    }

    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != p && !big[v])
            add(v, u, x);
    }
}

void dfs(int u, int fa, bool keep)
{
    int mx = -1, bigchild = -1;
    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && sz[v] > mx)
            mx = sz[v], bigchild = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && v != bigchild)
            dfs(v, u, 0);
    }
    if(bigchild != -1)
        dfs(bigchild, u, 1), big[bigchild] = 1;
    add(u, fa, 1);
    if(bigchild != -1)
        big[bigchild] = 0;
    ans[u] = sum;
    if(keep == 0)
        add(u, fa, -1), Max = sum = 0;
}

void getsz(int u, int fa)
{
    sz[u] = 1;
    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa)
        {
            getsz(v, u);
            sz[u] += sz[v];
        }
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    int u, v;
    for(int i = 1; i < n; i++)
    {
        scanf("%d%d", &u, &v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    getsz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
        printf("%lld ", ans[i]);
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 100005;

int n, Max, cnt[N], sz[N], col[N], big[N];
long long sum, ans[N];
vector<int> g[N];

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > Max)
    {
        sum = col[u];
        Max = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == Max)
    {
        sum += col[u];
    }

    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != p && !big[v])
            add(v, u, x);
    }
}

void dfs(int u, int fa, bool keep)
{
    int mx = -1, bigchild = -1;
    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && sz[v] > mx)
            mx = sz[v], bigchild = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && v != bigchild)
            dfs(v, u, 0);
    }
    if(bigchild != -1)
        dfs(bigchild, u, 1), big[bigchild] = 1;
    add(u, fa, 1);
    if(bigchild != -1)
        big[bigchild] = 0;
    ans[u] = sum;
    if(keep == 0)
        add(u, fa, -1), Max = sum = 0;
}

void getsz(int u, int fa)
{
    sz[u] = 1;
    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa)
        {
            getsz(v, u);
            sz[u] += sz[v];
        }
    }
}

void solve()
{
    cin >> n;
    int u, v;
    for(int i = 1; i <= n; i++) cin >> col[i];
    for(int i = 1; i < n; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
    getsz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
}

int main()
{
    solve();
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 1e5 + 100;

int col[maxn], sz[maxn], son[maxn], cnt[maxn];
int n, big, maxx;
long long ans[maxn], sum;
vector<int> G[maxn];

void get_son(int u, int p)
{
    sz[u] = 1;
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p)
        {
            get_son(v, u);
            sz[u] += sz[u];
            if(sz[v] > sz[son[u]])
                son[u] = v;
        }
    }
}

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > maxx)
    {
        sum = col[u];
        maxx = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == maxx)
    {
        sum += col[u];
    }

    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != big)
            add(v, u, x);
    }
}

void dfs(int u, int p, bool keep)
{
    int len = G[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = G[u][i];
        if(v != p && v != son[u])
            dfs(v, u, 0);
    }
    if(son[u])
    {
        dfs(son[u], u, 1);
        big = son[u];
    }
    add(u, p, 1);
    big = 0;
    ans[u] = sum;
    if(!keep)
    {
        add(u, p, -1);
        maxx = 0;
        sum = 0;
    }
}

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &col[i]);
    int u, v;
    for(int i = 0; i < n - 1; i++)
    {
        scanf("%d%d", &u, &v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    get_son(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++)
    {
        if(i != 1)
            printf(" ");
        printf("%lld", ans[i]);
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define N 100005
#define LL long long
int n, Max, cnt[N], sz[N], col[N], big[N];
LL sum, ans[N];
vector<int> g[N];
void add(int u, int fa, int x)
{
    cnt[col[u]]+=x;
    if(cnt[col[u]]>Max) sum=col[u], Max=cnt[col[u]];
    else if(cnt[col[u]]==Max) sum+=col[u];
    for(auto v:g[u])
        if(v!=fa&&!big[v])
            add(v, u, x);
}
void dfs(int u, int fa, bool keep)
{
    int mx=-1, bigchild=-1;
    for(auto v:g[u])
        if(v!=fa&&sz[v]>mx)
            mx=sz[v], bigchild=v;
    for(auto v:g[u])
        if(v!=fa&&v!=bigchild)
            dfs(v, u, 0);
    if(bigchild!=-1)
        dfs(bigchild, u, 1), big[bigchild]=1;
    add(u, fa, 1);
    if(bigchild!=-1)
        big[bigchild]=0;
    ans[u]=sum;
    if(keep==0)
        add(u, fa, -1), Max=sum=0;
}

void getsz(int u, int fa)
{
    sz[u]=1;
    for(auto v:g[u])
        if(v!=fa)
        {
            getsz(v, u);
            sz[u]+=sz[v];
        }
}
void solve()
{
    cin>>n;
    int u, v;
    for(int i=1; i<=n; i++) cin>>col[i];
    for(int i=1; i<n; i++) cin>>u>>v, g[u].push_back(v), g[v].push_back(u);
    getsz(1, 0);
    dfs(1, 0, 1);
    for(int i=1; i<=n; i++) cout << ans[i] << " ";
}
int main()
{
    solve();
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N = 100005;

int n, Max, cnt[N], sz[N], col[N], big[N];
long long sum, ans[N];
vector<int> g[N];

void add(int u, int p, int x)
{
    cnt[ col[u] ] += x;
    if(cnt[ col[u] ] > Max)
    {
        sum = col[u];
        Max = cnt[ col[u] ];
    }
    else if(cnt[ col[u] ] == Max)
    {
        sum += col[u];
    }

    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != p && !big[v])
            add(v, u, x);
    }
}

void dfs(int u, int fa, bool keep)
{
    int mx = -1, bigchild = -1;
    int len = g[u].size();
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && sz[v] > mx)
            mx = sz[v], bigchild = v;
    }
    for(int i = 0; i < len; i++)
    {
        int v = g[u][i];
        if(v != fa && v != bigchild)
            dfs(v, u, 0);
    }
    if(bigchild != -1)
        dfs(bigchild, u, 1), big[bigchild] = 1;
    add(u, fa, 1);
    if(bigchild != -1)
        big[bigchild] = 0;
    ans[u] = sum;
    if(keep == 0)
        add(u, fa, -1), Max = sum = 0;
}

void getsz(int u, int fa)
{
    sz[u] = 1;
    for(auto v : g[u])
        if(v != fa)
        {
            getsz(v, u);
            sz[u] += sz[v];
        }
}
void solve()
{
    cin >> n;
    int u, v;
    for(int i = 1; i <= n; i++) cin >> col[i];
    for(int i = 1; i < n; i++) cin >> u >> v, g[u].push_back(v), g[v].push_back(u);
    getsz(1, 0);
    dfs(1, 0, 1);
    for(int i = 1; i <= n; i++) cout << ans[i] << " ";
}
int main()
{
    solve();
    return 0;
}


#include <bits/stdc++.h>

#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
#pragma GCC target (sse, sse2, sse3, ssse3, sse4,popcnt,tune=native)
#pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define FOR(i, s, e) for(int i=s; i<e; i++)
#define loop(i, n) for(int i=0; i<n; i++)
#define CIN   ios_base::sync_with_stdio(0); cin.tie(0)
#define getint(n) scanf("%d", &n)
#define pb(a) push_back(a)
#define ll long long int
#define ll unsigned long long int
#define ld long double

#define SZ(a) (int)(a.size())
#define Read() freopen("input.txt", "r", stdin)
#define Write() freopen("output.txt", "w", stdout)
#define mem(a, v) memset(a, v, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define Unique(x)  x.erase(unique(all(x)), x.end())
//#define pi acos(-1.0)
#define pf printf
#define sf scanf
#define vec vector
#define mp make_pair
#define paii pair<int, int>
#define padd pair<dd, dd>
#define pall pair<ll, ll>
#define fr first
#define sc second
//#define endl "\n"
#define CASE(n) printf("Case %d: ",++n)
#define CASE_COUT cout<<"Case "<<++cas<<": "
#define vec vector

using namespace std;

//#define int long long

const int  MAXN = 1e5+ 47;

int n;
int a[MAXN];
vec<int> graf[MAXN];

void read() {
    cin >> n;
    loop(i, n) cin >> a[i];
    loop(i, n-1) {
        int p1, p2;
        cin >> p1 >> p2;
        p1--, p2--;
        graf[p1].pb(p2);
        graf[p2].pb(p1);
    }
}

vec<int> sz(MAXN, 0), tin(MAXN, 0), tout(MAXN, 0), rem(2*MAXN, MAXN-1);

int timer = 0;
void get_sz(int p1, int pr) {
    tin[p1] = timer, rem[timer] = p1;
    sz[p1]++, timer++;
    for(int p2 : graf[p1]) {
        if(p2 == pr) continue;
        get_sz(p2, p1);
        sz[p1] +=sz[p2];
    }
    tout[p1] = timer;
    timer++;
}

vec<int> cnt(MAXN, 0), ans(MAXN, 0), kek(MAXN, 0);
int gr = 0;

void dfs(int p1, int pr, bool x) {
    int mx = -1, big = -1;
    for(int p2: graf[p1]) {
        if(p2 == pr) continue;
        if(sz[p2] > mx) mx = sz[p2], big = p2;
    }

    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        dfs(p2, p1, 0);
    }
    int GR = 0;
    if(x == 1) GR = gr;
    if(big != -1) dfs(big, p1, 1);
    for(int p2 : graf[p1]) {
        if(p2 == pr || p2 == big) continue;
        for(int p = tin[p2]; p <= tout[p2]; ++p) {
            if(rem[p] == MAXN - 1) continue;
            ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
            cnt[a[rem[p]]]++;
            ans[cnt[a[rem[p]]]] += (a[rem[p]]);
            if(cnt[a[rem[p]]] > GR) GR = cnt[a[rem[p]]];
        }
    }
    ans[cnt[a[p1]]] -=(a[p1]);
    cnt[a[p1]]++;
    ans[cnt[a[p1]]] +=(a[p1]);
    if(cnt[a[p1]] > GR) GR = cnt[a[p1]];
    //cout << p1 << ": " << big << " " << GR << endl << "ans: ";
    //loop(i, n+1) cout << ans[i] << " ";
    //loop(i, n) if(ans[i] > 0) kek[p1] = ans[i];
    //cout << ": " << kek[p1];
    //cout << endl;
    kek[p1] = ans[GR];

    if(x == 0) {
        for(int p2 : graf[p1]) {
            if(p2 == pr) continue;
            for(int p = tin[p2]; p <= tout[p2]; ++p) {
                if(rem[p] == MAXN - 1) continue;
                ans[cnt[a[rem[p]]]] -= (a[rem[p]]);
                cnt[a[rem[p]]]--;
                ans[cnt[a[rem[p]]]] += (a[rem[p]]);
                if(ans[cnt[a[rem[p]]]] == 0) GR--;
            }
        }
        ans[cnt[a[p1]]] -=(a[p1]);
        cnt[a[p1]]--;
        ans[cnt[a[p1]]] +=(a[p1]);
        if(ans[cnt[a[p1]]] == 0) GR--;
    } else gr = GR;
    //loop(i, n+1) cout << ans[i] << " ";
    //cout << endl;
}

void solve(){
    get_sz(0, -1);
    //loop(i, 2*n) cout << rem[i] << " ";
    //cout << endl;

    loop(i, n) ans[0] += i + 1;
    dfs(0, -1, 1);
    loop(i, n) cout << kek[i] << " ";
    cout << endl;
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cout << fixed << setprecision(20);

    read();
    solve();

    /*int t;
    cin >> t;
    while(t--) {
        read();
        solve();
    }*/
}
/*
4
1 2 3 4
1 2
2 3
2 4

15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
*/