def numba_compile(numba_config):

    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":

        from numba import njit

        from numba.pycc import CC

        cc = CC("my_module")

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature)(func)

            cc.export(func.__name__, signature)(func)

        cc.compile()

        exit()

    elif os.name == "posix":

        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")

        for func, _ in numba_config:

            globals()[func.__name__] = vars()[func.__name__]

    else:

        from numba import njit

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature, cache=True)(func)

        print("compiled!", file=sys.stderr)



import numpy as np



def dfs1(v, G, Siz):

    siz = 1

    for u in G[v]:

        G[u].remove(v)

        siz += dfs1(u, G, Siz)

    Siz[v] = siz

    return siz



def dfs(v, N, C, G, Cnt, Siz, Ans):

    color = C[v]

    cnt_old = Cnt[color]

    siz = 1

    siz_up = N

    for u in G[v]:

        Cnt[color] = N - Siz[u]  # 上を塞ぐ

        s = dfs(u, N, C, G, Cnt, Siz, Ans)

        siz += s

        siz_up -= s

        Ans[color] += s * (siz_up - cnt_old)

    Ans[color] += siz_up - cnt_old

    Cnt[color] = cnt_old + siz

    return siz



def solve(N, C, AB):

    G = [[0]*0 for _ in range(N)]

    for i in range(N-1):

        a, b = AB[i]

        G[a].append(b)

        G[b].append(a)

    Cnt = np.zeros(N, dtype=np.int64)

    Ans = np.zeros(N, dtype=np.int64)

    Siz = np.zeros(N, dtype=np.int64)

    dfs1(0, G, Siz)

    dfs(0, N, C, G, Cnt, Siz, Ans)

    return Ans



numba_compile([

    [dfs1, "i8(i8,List(List(i8)),i8[:])"],

    [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],

    [solve, "i8[:](i8,i8[:],i8[:,:])"],

])



import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(505050)

N = int(input())

C = np.array(input().split(), dtype=np.int64) - 1

AB = np.array([list(map(int, input().split())) for _ in range(N-1)], dtype=np.int64) - 1

Ans = solve(N, C, AB)

print("\n".join(map(str, Ans)))

# オイラーツアーのテスト



def numba_compile(numba_config):

    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":

        from numba import njit

        from numba.pycc import CC

        cc = CC("my_module")

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature)(func)

            cc.export(func.__name__, signature)(func)

        cc.compile()

        exit()

    elif os.name == "posix":

        exec(f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}")

        for func, _ in numba_config:

            globals()[func.__name__] = vars()[func.__name__]

    else:

        from numba import njit

        for func, signature in numba_config:

            globals()[func.__name__] = njit(signature, cache=True)(func)

        print("compiled!", file=sys.stderr)



import numpy as np



def eular_tour(G, root):

    # G は破壊的に有向木にされる

    # グラフは 1-indexed が良い

    n = len(G)

    stack = [root]

    tour = [0] * 0

    first = np.full(n, -100, dtype=np.int64)

    last = np.full(n, -100, dtype=np.int64)

    while stack:

        v = stack.pop()

        if first[v] >= 0:

            last[v] = len(tour)

            tour.append(-v)  # 帰りがけの辺の表現をマイナス以外にしたい場合ここを変える

            continue

        first[v] = len(tour)

        tour.append(v)

        stack.append(v)

        for u in G[v]:

            G[u].remove(v)

            stack.append(u)

    tour = np.array(tour, dtype=np.int64)

    return tour, first, last



def dfs1(v, G, Siz):

    tour, first, last = eular_tour(G, 0)

    Siz[:] = last - first + 1 >> 1

    return 0



# def dfs1(v, G, Siz):

#     siz = 1

#     for u in G[v]:

#         G[u].remove(v)

#         siz += dfs1(u, G, Siz)

#     Siz[v] = siz

#     return siz



def dfs(v, N, C, G, Cnt, Siz, Ans):

    color = C[v]

    cnt_old = Cnt[color]

    siz = 1

    siz_up = N

    for u in G[v]:

        Cnt[color] = N - Siz[u]  # 上を塞ぐ

        s = dfs(u, N, C, G, Cnt, Siz, Ans)

        siz += s

        siz_up -= s

        Ans[color] += s * (siz_up - cnt_old)

    Ans[color] += siz_up - cnt_old

    Cnt[color] = cnt_old + siz

    return siz



def solve(N, C, AB):

    G = [[0]*0 for _ in range(N)]

    for i in range(N-1):

        a, b = AB[i]

        G[a].append(b)

        G[b].append(a)

    Cnt = np.zeros(N, dtype=np.int64)

    Ans = np.zeros(N, dtype=np.int64)

    Siz = np.zeros(N, dtype=np.int64)

    dfs1(0, G, Siz)

    dfs(0, N, C, G, Cnt, Siz, Ans)

    return Ans



numba_compile([

    [eular_tour, "Tuple((i8[:],i8[:],i8[:]))(List(List(i8)),i8)"],

    [dfs1, "i8(i8,List(List(i8)),i8[:])"],

    [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],

    [solve, "i8[:](i8,i8[:],i8[:,:])"],

])



import sys

input = sys.stdin.buffer.readline

sys.setrecursionlimit(505050)

N = int(input())

C = np.array(input().split(), dtype=np.int64) - 1

AB = np.array([list(map(int, input().split())) for _ in range(N-1)], dtype=np.int64) - 1

Ans = solve(N, C, AB)

print("\n".join(map(str, Ans)))

def numba_compile(numba_config):
    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC

        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(
            f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}"
        )
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit

        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)


import numpy as np


def dfs1(v, G, Siz):
    siz = 1
    for u in G[v]:
        G[u].remove(v)
        siz += dfs1(u, G, Siz)
    Siz[v] = siz
    return siz


def dfs(v, N, C, G, Cnt, Siz, Ans):
    color = C[v]
    cnt_old = Cnt[color]
    siz = 1
    siz_up = N
    for u in G[v]:
        Cnt[color] = N - Siz[u]  # 上を塞ぐ
        s = dfs(u, N, C, G, Cnt, Siz, Ans)
        siz += s
        siz_up -= s
        Ans[color] += s * (siz_up - cnt_old)
    Ans[color] += siz_up - cnt_old
    Cnt[color] = cnt_old + siz
    return siz


def solve(N, C, AB):
    G = [[0] * 0 for _ in range(N)]
    for i in range(N - 1):
        a, b = AB[i]
        G[a].append(b)
        G[b].append(a)
    Cnt = np.zeros(N, dtype=np.int64)
    Ans = np.zeros(N, dtype=np.int64)
    Siz = np.zeros(N, dtype=np.int64)
    dfs1(0, G, Siz)
    dfs(0, N, C, G, Cnt, Siz, Ans)
    return Ans


numba_compile(
    [
        [dfs1, "i8(i8,List(List(i8)),i8[:])"],
        [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],
        [solve, "i8[:](i8,i8[:],i8[:,:])"],
    ]
)
import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(505050)
N = int(input())
C = np.array(input().split(), dtype=np.int64) - 1
AB = (
    np.array([list(map(int, input().split())) for _ in range(N - 1)], dtype=np.int64)
    - 1
)
Ans = solve(N, C, AB)
print("\n".join(map(str, Ans)))

# オイラーツアーのテスト
def numba_compile(numba_config):
    import os, sys

    if sys.argv[-1] == "ONLINE_JUDGE":
        from numba import njit
        from numba.pycc import CC

        cc = CC("my_module")
        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature)(func)
            cc.export(func.__name__, signature)(func)
        cc.compile()
        exit()
    elif os.name == "posix":
        exec(
            f"from my_module import {','.join(func.__name__ for func, _ in numba_config)}"
        )
        for func, _ in numba_config:
            globals()[func.__name__] = vars()[func.__name__]
    else:
        from numba import njit

        for func, signature in numba_config:
            globals()[func.__name__] = njit(signature, cache=True)(func)
        print("compiled!", file=sys.stderr)


import numpy as np


def eular_tour(G, root):
    # G は破壊的に有向木にされる
    # グラフは 1-indexed が良い
    n = len(G)
    stack = [root]
    tour = [0] * 0
    first = np.full(n, -100, dtype=np.int64)
    last = np.full(n, -100, dtype=np.int64)
    while stack:
        v = stack.pop()
        if first[v] >= 0:
            last[v] = len(tour)
            tour.append(-v)  # 帰りがけの辺の表現をマイナス以外にしたい場合ここを変える
            continue
        first[v] = len(tour)
        tour.append(v)
        stack.append(v)
        for u in G[v]:
            G[u].remove(v)
            stack.append(u)
    tour = np.array(tour, dtype=np.int64)
    return tour, first, last


def dfs1(v, G, Siz):
    tour, first, last = eular_tour(G, 0)
    Siz[:] = last - first + 1 >> 1
    return 0


# def dfs1(v, G, Siz):
#     siz = 1
#     for u in G[v]:
#         G[u].remove(v)
#         siz += dfs1(u, G, Siz)
#     Siz[v] = siz
#     return siz
def dfs(v, N, C, G, Cnt, Siz, Ans):
    color = C[v]
    cnt_old = Cnt[color]
    siz = 1
    siz_up = N
    for u in G[v]:
        Cnt[color] = N - Siz[u]  # 上を塞ぐ
        s = dfs(u, N, C, G, Cnt, Siz, Ans)
        siz += s
        siz_up -= s
        Ans[color] += s * (siz_up - cnt_old)
    Ans[color] += siz_up - cnt_old
    Cnt[color] = cnt_old + siz
    return siz


def solve(N, C, AB):
    G = [[0] * 0 for _ in range(N)]
    for i in range(N - 1):
        a, b = AB[i]
        G[a].append(b)
        G[b].append(a)
    Cnt = np.zeros(N, dtype=np.int64)
    Ans = np.zeros(N, dtype=np.int64)
    Siz = np.zeros(N, dtype=np.int64)
    dfs1(0, G, Siz)
    dfs(0, N, C, G, Cnt, Siz, Ans)
    return Ans


numba_compile(
    [
        [eular_tour, "Tuple((i8[:],i8[:],i8[:]))(List(List(i8)),i8)"],
        [dfs1, "i8(i8,List(List(i8)),i8[:])"],
        [dfs, "i8(i8,i8,i8[:],List(List(i8)),i8[:],i8[:],i8[:])"],
        [solve, "i8[:](i8,i8[:],i8[:,:])"],
    ]
)
import sys

input = sys.stdin.buffer.readline
sys.setrecursionlimit(505050)
N = int(input())
C = np.array(input().split(), dtype=np.int64) - 1
AB = (
    np.array([list(map(int, input().split())) for _ in range(N - 1)], dtype=np.int64)
    - 1
)
Ans = solve(N, C, AB)
print("\n".join(map(str, Ans)))

+# オイラーツアーのテスト -def dfs1(v, G, Siz): -    siz = 1 -    for u in G[v]: -        G[u].remove(v) -        siz += dfs1(u, G, Siz) -    Siz[v] = siz -    return siz +def eular_tour(G, root): +    # G は破壊的に有向木にされる +    # グラフは 1-indexed が良い +    n = len(G) +    stack = [root] +    tour = [0] * 0 +    first = np.full(n, -100, dtype=np.int64) +    last = np.full(n, -100, dtype=np.int64) +    while stack: +        v = stack.pop() +        if first[v] >= 0: +            last[v] = len(tour) +            tour.append(-v)  # 帰りがけの辺の表現をマイナス以外にしたい場合ここを変える +            continue +        first[v] = len(tour) +        tour.append(v) +        stack.append(v) +        for u in G[v]: +            G[u].remove(v) +            stack.append(u) +    tour = np.array(tour, dtype=np.int64) +    return tour, first, last +def dfs1(v, G, Siz): +    tour, first, last = eular_tour(G, 0) +    Siz[:] = last - first + 1 >> 1 +    return 0 + + +# def dfs1(v, G, Siz): +#     siz = 1 +#     for u in G[v]: +#         G[u].remove(v) +#         siz += dfs1(u, G, Siz) +#     Siz[v] = siz +#     return siz +        [eular_tour, "Tuple((i8[:],i8[:],i8[:]))(List(List(i8)),i8)"],