from collections import deque

n = int(input())
ed = [[] for _ in range(n)]
al,be = [],[]
for _ in range(n-1):
    a,b = map(int,input().split())
    al.append(a-1)
    be.append(b-1)
    ed[a-1].append(b-1)
    ed[b-1].append(a-1)
    
c = list(map(int,input().split()))
c.sort(reverse = True)

ans = [0]*n

q = deque()
q.append(0)
used = [False]*n
used[0] = True
ans[0] = c[0]
j = 1
 
while q:
    p = q.popleft()
    for i in ed[p]:
        if not used[i]:
            used[i] = True
            ans[i] = c[j]
            j += 1
            q.append(i)
        
sc = 0
for i in range(n-1):
    sc += min(ans[al[i]],ans[be[i]])
    
print(sc)
print(*ans) 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) [] ed = [[] for _ in range(n)] ed [[] for _ in range(n)] al,be = [],[] al [] be [] for _ in range(n-1):
    a,b = map(int,input().split())
    al.append(a-1)
    be.append(b-1)
    ed[a-1].append(b-1)
    ed[b-1].append(a-1)
     _ 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 al.append(a-1) al.append al append a-1 a 1 be.append(b-1) be.append be append b-1 b 1 ed[a-1].append(b-1) [a-1].append [a-1] ed a-1 a 1 append b-1 b 1 ed[b-1].append(a-1) [b-1].append [b-1] ed b-1 b 1 append a-1 a 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 ans = [0]*n ans [0]*n [0] 0 n q = deque() q deque() deque q.append(0) q.append q append 0 used = [False]*n used [False]*n [False] False n used[0] = True used[0] used 0 True ans[0] = c[0] ans[0] ans 0 c[0] c 0 j = 1 j 1 while q:
    p = q.popleft()
    for i in ed[p]:
        if not used[i]:
            used[i] = True
            ans[i] = c[j]
            j += 1
            q.append(i)
         q p = q.popleft() p q.popleft() q.popleft q popleft for i in ed[p]:
        if not used[i]:
            used[i] = True
            ans[i] = c[j]
            j += 1
            q.append(i)
         i ed[p] ed p if not used[i]:
            used[i] = True
            ans[i] = c[j]
            j += 1
            q.append(i)
         not used[i] used[i] used i used[i] = True used[i] used i True ans[i] = c[j] ans[i] ans i c[j] c j j += 1 j 1 q.append(i) q.append q append i sc = 0 sc 0 for i in range(n-1):
    sc += min(ans[al[i]],ans[be[i]])
     i range(n-1) range n-1 n 1 sc += min(ans[al[i]],ans[be[i]]) sc min(ans[al[i]],ans[be[i]]) min ans[al[i]] ans al[i] al i ans[be[i]] ans be[i] be i print(sc) print sc print(*ans) print *ans ans ans = [0]*n [0]*n ans al,be = [],[] [] al n = int(input()) int(input()) n sc = 0 0 sc j += 1 1 j b = map(int,input().split()) map(int,input().split()) b used = [False]*n [False]*n used ed = [[] for _ in range(n)] [[] for _ in range(n)] ed sc += min(ans[al[i]],ans[be[i]]) min(ans[al[i]],ans[be[i]]) sc p = q.popleft() q.popleft() p j = 1 1 j a,b = map(int,input().split()) map(int,input().split()) a c = list(map(int,input().split())) list(map(int,input().split())) c q = deque() deque() q be = [],[] [] be
from collections import deque

n = int(input())
ed = [[] for _ in range(n)]
al,be = [],[]
for _ in range(n-1):
    a,b = map(int,input().split())
    al.append(a-1)
    be.append(b-1)
    ed[a-1].append(b-1)
    ed[b-1].append(a-1)
    
c = list(map(int,input().split()))
c.sort(reverse = True)

ans = [0]*n

q = deque()
q.append(0)
used = [False]*n
used[0] = True
ans[0] = c[0]
j = 1
 
while q:
    p = q.popleft()
    for i in ed[p]:
        if not used[i]:
            used[i] = True
            ans[i] = c[j]
            j += 1
            q.append(i)
        
sc = 0
for i in range(n-1):
    sc += min(ans[al[i]],ans[be[i]])
    
print(sc)
print(*ans)