import sys
input=sys.stdin.readline
n=int(input())

l=[[] for _ in range(n)]
ab=[]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    l[a].append(b)
    l[b].append(a)
    ab.append((a,b))

ans=[0]*n

from collections import deque

c=list(map(int,input().split()))
c.sort(reverse=True)
c=deque(c)
q=deque()
q.append([0])
while q:
    ll=q.popleft()
    for v in ll:
        if ans[v]!=0:
            continue
        ans[v]=c.popleft()
        q.append(l[v])

x=0
for a,b in ab:
    x+=min(ans[a],ans[b])

print(x)
print(*ans) import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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) [] l=[[] for _ in range(n)] l [[] for _ in range(n)] ab=[] ab [] for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    l[a].append(b)
    l[b].append(a)
    ab.append((a,b)) _ 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 l[a].append(b) [a].append [a] l a append b l[b].append(a) [b].append [b] l b append a ab.append((a,b)) ab.append ab append (a,b) a b ans=[0]*n ans [0]*n [0] 0 n from collections import deque 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 c=deque(c) c deque(c) deque c q=deque() q deque() deque q.append([0]) q.append q append [0] 0 while q:
    ll=q.popleft()
    for v in ll:
        if ans[v]!=0:
            continue
        ans[v]=c.popleft()
        q.append(l[v]) q ll=q.popleft() ll q.popleft() q.popleft q popleft for v in ll:
        if ans[v]!=0:
            continue
        ans[v]=c.popleft()
        q.append(l[v]) v ll if ans[v]!=0:
            continue
         ans[v]!=0 ans[v] ans v 0 continue ans[v]=c.popleft() ans[v] ans v c.popleft() c.popleft c popleft q.append(l[v]) q.append q append l[v] l v x=0 x 0 for a,b in ab:
    x+=min(ans[a],ans[b]) a b ab x+=min(ans[a],ans[b]) x min(ans[a],ans[b]) min ans[a] ans a ans[b] ans b print(x) print x print(*ans) print *ans ans b=map(int,input().split()) map(int,input().split()) b ans=[0]*n [0]*n ans c=deque(c) deque(c) c x=0 0 x c=list(map(int,input().split())) list(map(int,input().split())) c n=int(input()) int(input()) n q=deque() deque() q ab=[] [] ab x+=min(ans[a],ans[b]) min(ans[a],ans[b]) x b-=1 1 b ll=q.popleft() q.popleft() ll a-=1 1 a l=[[] for _ in range(n)] [[] for _ in range(n)] l a,b=map(int,input().split()) map(int,input().split()) a input=sys.stdin.readline sys.stdin.readline input
import sys
input=sys.stdin.readline
n=int(input())

l=[[] for _ in range(n)]
ab=[]
for _ in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    l[a].append(b)
    l[b].append(a)
    ab.append((a,b))

ans=[0]*n

from collections import deque

c=list(map(int,input().split()))
c.sort(reverse=True)
c=deque(c)
q=deque()
q.append([0])
while q:
    ll=q.popleft()
    for v in ll:
        if ans[v]!=0:
            continue
        ans[v]=c.popleft()
        q.append(l[v])

x=0
for a,b in ab:
    x+=min(ans[a],ans[b])

print(x)
print(*ans)
