N = int(eval(input()))

AB = [list(map(int,input().split())) for _ in [0]*(N-1)]

*C, = list(map(int,input().split()))



connect = [[] for _ in [0]*N]

for a,b in AB:

    connect[a-1].append(b-1)

    connect[b-1].append(a-1)

d = [-1]*N

d[0] = 0

q = [0]

while q:

    qq = []

    for i in q:

        for j in connect[i]:

            if d[j] == -1:

                d[j] = d[i]+1

                qq.append(j)

    q = qq



o = sorted(zip(d,list(range(N))),key =lambda x:-x[0])

C.sort()

ans = [0]*N

for j,tmp in enumerate(o):

    d,i = tmp

    ans[i] = C[j]

print((sum(C)-C[-1]))

print((*ans))
N = int(eval(input()))

AB = [list(map(int,input().split())) for _ in [0]*(N-1)]

*C, = sorted(map(int,input().split()))

print((sum(C[:-1])))

E = [[] for _ in [0]*N]

d = [0]*N

for a,b in AB:

    E[a-1].append(b-1)

    E[b-1].append(a-1)



for i in range(N):

    if len(E[i])>1:break

q = [i]

while q:

    i = q.pop()

    d[i] = C.pop()

    for j in E[i]:

        if d[j] != 0:continue

        q.append(j)

print((*d))
N = int(eval(input()))
AB = [list(map(int, input().split())) for _ in [0] * (N - 1)]
(*C,) = list(map(int, input().split()))
connect = [[] for _ in [0] * N]
for a, b in AB:
    connect[a - 1].append(b - 1)
    connect[b - 1].append(a - 1)
d = [-1] * N
d[0] = 0
q = [0]
while q:
    qq = []
    for i in q:
        for j in connect[i]:
            if d[j] == -1:
                d[j] = d[i] + 1
                qq.append(j)
    q = qq
o = sorted(zip(d, list(range(N))), key=lambda x: -x[0])
C.sort()
ans = [0] * N
for j, tmp in enumerate(o):
    d, i = tmp
    ans[i] = C[j]
print((sum(C) - C[-1]))
print((*ans))

N = int(eval(input()))
AB = [list(map(int, input().split())) for _ in [0] * (N - 1)]
(*C,) = sorted(map(int, input().split()))
print((sum(C[:-1])))
E = [[] for _ in [0] * N]
d = [0] * N
for a, b in AB:
    E[a - 1].append(b - 1)
    E[b - 1].append(a - 1)
for i in range(N):
    if len(E[i]) > 1:
        break
q = [i]
while q:
    i = q.pop()
    d[i] = C.pop()
    for j in E[i]:
        if d[j] != 0:
            continue
        q.append(j)
print((*d))

-(*C,) = list(map(int, input().split())) -connect = [[] for _ in [0] * N] +(*C,) = sorted(map(int, input().split())) +print((sum(C[:-1]))) +E = [[] for _ in [0] * N] +d = [0] * N -    connect[a - 1].append(b - 1) -    connect[b - 1].append(a - 1) -d = [-1] * N -d[0] = 0 -q = [0] +    E[a - 1].append(b - 1) +    E[b - 1].append(a - 1) +for i in range(N): +    if len(E[i]) > 1: +        break +q = [i] -    qq = [] -    for i in q: -        for j in connect[i]: -            if d[j] == -1: -                d[j] = d[i] + 1 -                qq.append(j) -    q = qq -o = sorted(zip(d, list(range(N))), key=lambda x: -x[0]) -C.sort() -ans = [0] * N -for j, tmp in enumerate(o): -    d, i = tmp -    ans[i] = C[j] -print((sum(C) - C[-1])) -print((*ans)) +    i = q.pop() +    d[i] = C.pop() +    for j in E[i]: +        if d[j] != 0: +            continue +        q.append(j) +print((*d))