from collections import deque
N = int(input())
T = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(lambda x:int(x)-1,input().split())
    T[a].append(b)
    T[b].append(a)
P = list(map(int,input().split()))
P.sort(reverse=True)
A = [-1]*N
v = -1
for i in range(N):
    if len(T[i])==1:
        v = i
        break
Q = deque([v])
p_index = 0
while Q:
    v = Q.pop()
    A[v] = P[p_index]
    p_index+=1
    for c in T[v]:
        if A[c]==-1:
            Q.append(c)
print(sum(P[1:]))
print(*A) from collections import deque N = int(input()) N int(input()) int input() input [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] T = [[] for i in range(N)] T [[] for i in range(N)] for i in range(N-1):
    a,b = map(lambda x:int(x)-1,input().split())
    T[a].append(b)
    T[b].append(a) i range(N-1) range N-1 N 1 a,b = map(lambda x:int(x)-1,input().split()) a map(lambda x:int(x)-1,input().split()) map lambda x:int(x)-1 int(x)-1 int(x) int x 1 x input().split() ().split () input split b T[a].append(b) [a].append [a] T a append b T[b].append(a) [b].append [b] T b append a P = list(map(int,input().split())) P list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split P.sort(reverse=True) P.sort P sort reverse=True reverse True A = [-1]*N A [-1]*N [-1] -1 N v = -1 v -1 for i in range(N):
    if len(T[i])==1:
        v = i
        break i range(N) range N if len(T[i])==1:
        v = i
        break len(T[i])==1 len(T[i]) len T[i] T i 1 v = i v i break Q = deque([v]) Q deque([v]) deque [v] v p_index = 0 p_index 0 while Q:
    v = Q.pop()
    A[v] = P[p_index]
    p_index+=1
    for c in T[v]:
        if A[c]==-1:
            Q.append(c) Q v = Q.pop() v Q.pop() Q.pop Q pop A[v] = P[p_index] A[v] A v P[p_index] P p_index p_index+=1 p_index 1 for c in T[v]:
        if A[c]==-1:
            Q.append(c) c T[v] T v if A[c]==-1:
            Q.append(c) A[c]==-1 A[c] A c -1 Q.append(c) Q.append Q append c print(sum(P[1:])) print sum(P[1:]) sum P[1:] P 1: 1 print(*A) print *A A v = -1 -1 v p_index+=1 1 p_index v = Q.pop() Q.pop() v T = [[] for i in range(N)] [[] for i in range(N)] T A = [-1]*N [-1]*N A P = list(map(int,input().split())) list(map(int,input().split())) P Q = deque([v]) deque([v]) Q a,b = map(lambda x:int(x)-1,input().split()) map(lambda x:int(x)-1,input().split()) a N = int(input()) int(input()) N v = i i v b = map(lambda x:int(x)-1,input().split()) map(lambda x:int(x)-1,input().split()) b p_index = 0 0 p_index
from collections import deque
N = int(input())
T = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(lambda x:int(x)-1,input().split())
    T[a].append(b)
    T[b].append(a)
P = list(map(int,input().split()))
P.sort(reverse=True)
A = [-1]*N
v = -1
for i in range(N):
    if len(T[i])==1:
        v = i
        break
Q = deque([v])
p_index = 0
while Q:
    v = Q.pop()
    A[v] = P[p_index]
    p_index+=1
    for c in T[v]:
        if A[c]==-1:
            Q.append(c)
print(sum(P[1:]))
print(*A)