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(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 sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 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]) DFS 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]) color[num]=="white" color[num] color num "white" A[num]=C.pop() A[num] A num C.pop() C.pop C pop point +=min(A[num],pre) point min(A[num],pre) min A[num] A num pre color[num]="black" color[num] color num "black" for i in tree[num]:
            DFS(i,A[num]) i tree[num] tree num DFS(i,A[num]) DFS i A[num] A num num num pre pre n=int(input()) n int(input()) int input() input list(map(int,input().split())) for _ in range(n-1) for _ in range(n-1) _ range(n-1) range n-1 n 1 for _ in range(n-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split AB=[list(map(int,input().split())) for _ in range(n-1)] AB [list(map(int,input().split())) for _ in range(n-1)] C=sorted(list(map(int,input().split()))) C sorted(list(map(int,input().split()))) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split "white" for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) "white" color=["white" for _ in range(n+1)] color ["white" for _ in range(n+1)] [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] tree=[[] 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 b AB tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree b append a 0 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) 0 A=[0 for _ in range(n+1)] A [0 for _ in range(n+1)] point=0 point 0 DFS(1,0) DFS 1 0 print(point) print point print(*A[1:]) print *A[1:] A[1:] A 1: 1 C=sorted(list(map(int,input().split()))) sorted(list(map(int,input().split()))) C 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]) 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]) DFS AB=[list(map(int,input().split())) for _ in range(n-1)] [list(map(int,input().split())) for _ in range(n-1)] AB tree=[[] for _ in range(n+1)] [[] for _ in range(n+1)] tree color=["white" for _ in range(n+1)] ["white" for _ in range(n+1)] color point=0 0 point n=int(input()) int(input()) n A=[0 for _ in range(n+1)] [0 for _ in range(n+1)] A
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(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:])