from collections import deque
N=int(input())
G=[[]for _ in range(N)]
cnt=[0]*N
for i in range(N-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)
    cnt[a]+=1
    cnt[b]+=1

C=list(map(int,input().split()))
C.sort(reverse=True)

root=0
v=0
for i in range(N):
    if v>cnt[i]:
        root=i
        v=cnt[i]

def bfs(s):
    que=deque()
    que.append(s)
    nodes=[-1]*N
    k=0
    ans=0
    while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
    print(sum(C[1:]))
    print(*nodes)

bfs(root) from collections import deque N=int(input()) N int(input()) int input() input []for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] G=[[]for _ in range(N)] G [[]for _ in range(N)] cnt=[0]*N cnt [0]*N [0] 0 N for i in range(N-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)
    cnt[a]+=1
    cnt[b]+=1 i range(N-1) range N-1 N 1 a,b=map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b a-=1 a 1 b-=1 b 1 G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a cnt[a]+=1 cnt[a] cnt a 1 cnt[b]+=1 cnt[b] cnt b 1 C=list(map(int,input().split())) C list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split C.sort(reverse=True) C.sort C sort reverse=True reverse True root=0 root 0 v=0 v 0 for i in range(N):
    if v>cnt[i]:
        root=i
        v=cnt[i] i range(N) range N if v>cnt[i]:
        root=i
        v=cnt[i] v>cnt[i] v cnt[i] cnt i root=i root i v=cnt[i] v cnt[i] cnt i def bfs(s):
    que=deque()
    que.append(s)
    nodes=[-1]*N
    k=0
    ans=0
    while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
    print(sum(C[1:]))
    print(*nodes) bfs que=deque() que deque() deque que.append(s) que.append que append s nodes=[-1]*N nodes [-1]*N [-1] -1 N k=0 k 0 ans=0 ans 0 while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
     len(que)>0 len(que) len que 0 q=que.pop() q que.pop() que.pop que pop nodes[q]=C[k] nodes[q] nodes q C[k] C k k+=1 k 1 for v in G[q]:
            if nodes[v]<0:
                que.append(v)
     v G[q] G q if nodes[v]<0:
                que.append(v)
     nodes[v]<0 nodes[v] nodes v 0 que.append(v) que.append que append v print(sum(C[1:])) print sum(C[1:]) sum C[1:] C 1: 1 print(*nodes) print *nodes nodes s s bfs(root) bfs root def bfs(s):
    que=deque()
    que.append(s)
    nodes=[-1]*N
    k=0
    ans=0
    while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
    print(sum(C[1:]))
    print(*nodes) def bfs(s):
    que=deque()
    que.append(s)
    nodes=[-1]*N
    k=0
    ans=0
    while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
    print(sum(C[1:]))
    print(*nodes) bfs a-=1 1 a root=0 0 root a,b=map(int,input().split()) map(int,input().split()) a cnt=[0]*N [0]*N cnt C=list(map(int,input().split())) list(map(int,input().split())) C v=cnt[i] cnt[i] v N=int(input()) int(input()) N b-=1 1 b G=[[]for _ in range(N)] [[]for _ in range(N)] G root=i i root v=0 0 v b=map(int,input().split()) map(int,input().split()) b
from collections import deque
N=int(input())
G=[[]for _ in range(N)]
cnt=[0]*N
for i in range(N-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    G[a].append(b)
    G[b].append(a)
    cnt[a]+=1
    cnt[b]+=1

C=list(map(int,input().split()))
C.sort(reverse=True)

root=0
v=0
for i in range(N):
    if v>cnt[i]:
        root=i
        v=cnt[i]

def bfs(s):
    que=deque()
    que.append(s)
    nodes=[-1]*N
    k=0
    ans=0
    while len(que)>0:
        q=que.pop()
        nodes[q]=C[k]
        k+=1

        for v in G[q]:
            if nodes[v]<0:
                que.append(v)
    print(sum(C[1:]))
    print(*nodes)

bfs(root)