import sys

sys.setrecursionlimit(10**6)





def DFS(num,pre):

    global point

    if color[num]=="white":

        ans[num]=C.pop()

        point +=min(ans[num],pre)

        color[num]="black"

        for i in T[num]:

            DFS(i,ans[num])





n=int(eval(input()))

AB=[list(map(int,input().split())) for _ in range(n-1)]

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

color=["white" for _ in range(n)]



T=[[] for _ in range(n)]

for a,b in AB:

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

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



ans=[0 for _ in range(n)]

point=0

DFS(0,0)



print(point)

print((*ans))
import sys

sys.setrecursionlimit(10**6)





def DFS(num,pre):

    global point

    if color[num]=="white":

        A[num]=C.pop()

        point +=min(A[num],pre)

        color[num]="black"

        for i in tree[num]:

            DFS(i,A[num])





n=int(eval(input()))

AB=[list(map(int,input().split())) for _ in range(n-1)]

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



color=["white" for _ in range(n+1)]

tree=[[] for _ in range(n+1)]

for a,b in AB:

    tree[a].append(b)

    tree[b].append(a)



A=[0 for _ in range(n+1)]

point=0

DFS(1,0)



print(point)

print((*A[1:]))
import sys

sys.setrecursionlimit(10**6)


def DFS(num, pre):
    global point
    if color[num] == "white":
        ans[num] = C.pop()
        point += min(ans[num], pre)
        color[num] = "black"
        for i in T[num]:
            DFS(i, ans[num])


n = int(eval(input()))
AB = [list(map(int, input().split())) for _ in range(n - 1)]
C = sorted(list(map(int, input().split())))
color = ["white" for _ in range(n)]
T = [[] for _ in range(n)]
for a, b in AB:
    T[a - 1].append(b - 1)
    T[b - 1].append(a - 1)
ans = [0 for _ in range(n)]
point = 0
DFS(0, 0)
print(point)
print((*ans))

import sys

sys.setrecursionlimit(10**6)


def DFS(num, pre):
    global point
    if color[num] == "white":
        A[num] = C.pop()
        point += min(A[num], pre)
        color[num] = "black"
        for i in tree[num]:
            DFS(i, A[num])


n = int(eval(input()))
AB = [list(map(int, input().split())) for _ in range(n - 1)]
C = sorted(list(map(int, input().split())))
color = ["white" for _ in range(n + 1)]
tree = [[] for _ in range(n + 1)]
for a, b in AB:
    tree[a].append(b)
    tree[b].append(a)
A = [0 for _ in range(n + 1)]
point = 0
DFS(1, 0)
print(point)
print((*A[1:]))

-        ans[num] = C.pop() -        point += min(ans[num], pre) +        A[num] = C.pop() +        point += min(A[num], pre) -        for i in T[num]: -            DFS(i, ans[num]) +        for i in tree[num]: +            DFS(i, A[num]) -color = ["white" for _ in range(n)] -T = [[] for _ in range(n)] +color = ["white" for _ in range(n + 1)] +tree = [[] for _ in range(n + 1)] -    T[a - 1].append(b - 1) -    T[b - 1].append(a - 1) -ans = [0 for _ in range(n)] +    tree[a].append(b) +    tree[b].append(a) +A = [0 for _ in range(n + 1)] -DFS(0, 0) +DFS(1, 0) -print((*ans)) +print((*A[1:]))