from collections import deque

n=int(input())

g=[[] for _ in range(n+1)]

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

c=sorted(list(map(int,input().split())))
print(sum(c[:-1]))

q=deque([1])
visit=[0]*(n+1)
ans=['']*(n+1)

while len(q)>0:
    u=q.popleft()

    visit[u]=1

    ans[u]=str(c.pop())

    for v in g[u]:
        if visit[v]:
            continue

        q.append(v)

print(' '.join(ans[1:])) from collections import deque n=int(input()) n int(input()) int input() input [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] g=[[] for _ in range(n+1)] g [[] for _ in range(n+1)] for _ in range(n-1):
    a,b=list(map(int,input().split()))
    g[a].append(b)
    g[b].append(a) _ range(n-1) range n-1 n 1 a,b=list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split b g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a 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 print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 q=deque([1]) q deque([1]) deque [1] 1 visit=[0]*(n+1) visit [0]*(n+1) [0] 0 n+1 n 1 ans=['']*(n+1) ans ['']*(n+1) [''] '' n+1 n 1 while len(q)>0:
    u=q.popleft()

    visit[u]=1

    ans[u]=str(c.pop())

    for v in g[u]:
        if visit[v]:
            continue

        q.append(v) len(q)>0 len(q) len q 0 u=q.popleft() u q.popleft() q.popleft q popleft visit[u]=1 visit[u] visit u 1 ans[u]=str(c.pop()) ans[u] ans u str(c.pop()) str c.pop() c.pop c pop for v in g[u]:
        if visit[v]:
            continue

        q.append(v) v g[u] g u if visit[v]:
            continue

         visit[v] visit v continue q.append(v) q.append q append v print(' '.join(ans[1:])) print ' '.join(ans[1:]) ' '.join ' ' join ans[1:] ans 1: 1 n=int(input()) int(input()) n b=list(map(int,input().split())) list(map(int,input().split())) b ans=['']*(n+1) ['']*(n+1) ans q=deque([1]) deque([1]) q a,b=list(map(int,input().split())) list(map(int,input().split())) a u=q.popleft() q.popleft() u visit=[0]*(n+1) [0]*(n+1) visit c=sorted(list(map(int,input().split()))) sorted(list(map(int,input().split()))) c g=[[] for _ in range(n+1)] [[] for _ in range(n+1)] g
from collections import deque

n=int(input())

g=[[] for _ in range(n+1)]

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

c=sorted(list(map(int,input().split())))
print(sum(c[:-1]))

q=deque([1])
visit=[0]*(n+1)
ans=['']*(n+1)

while len(q)>0:
    u=q.popleft()

    visit[u]=1

    ans[u]=str(c.pop())

    for v in g[u]:
        if visit[v]:
            continue

        q.append(v)

print(' '.join(ans[1:]))