from collections import deque
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
c=list(map(int,input().split()))
c.sort()
connection=[[] for i in range(n)]
ans=[0 for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)
next=[0]
next=deque(next)
check=[-1 for i in range(n)]
check[0]=1
ct=n
while ct>0:
  ct-=1
  x=next.popleft()
  ans[x]=str(c[ct])
  for i in range(len(connection[x])):
    if check[connection[x][i]]==-1:
      next.append(connection[x][i])
      check[connection[x][i]]=1
print(sum(c)-c[n-1])
print(' '.join(ans)) from collections import deque n=int(input()) n int(input()) int input() input list(map(int,input().split())) for i in range(n-1) for i in range(n-1) i range(n-1) range n-1 n 1 for i in range(n-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split l=[list(map(int,input().split())) for i in range(n-1)] l [list(map(int,input().split())) for i in range(n-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() c.sort c sort [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] connection=[[] for i in range(n)] connection [[] for i in range(n)] 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 ans=[0 for i in range(n)] ans [0 for i in range(n)] for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1) i range(n-1) range n-1 n 1 connection[l[i][0]-1].append(l[i][1]-1) [l[i][0]-1].append [l[i][0]-1] connection l[i][0]-1 l[i][0] [i] l i 0 1 append l[i][1]-1 l[i][1] [i] l i 1 1 connection[l[i][1]-1].append(l[i][0]-1) [l[i][1]-1].append [l[i][1]-1] connection l[i][1]-1 l[i][1] [i] l i 1 1 append l[i][0]-1 l[i][0] [i] l i 0 1 next=[0] next [0] 0 next=deque(next) next deque(next) deque next -1 for i in range(n) for i in range(n) i range(n) range n for i in range(n) -1 check=[-1 for i in range(n)] check [-1 for i in range(n)] check[0]=1 check[0] check 0 1 ct=n ct n while ct>0:
  ct-=1
  x=next.popleft()
  ans[x]=str(c[ct])
  for i in range(len(connection[x])):
    if check[connection[x][i]]==-1:
      next.append(connection[x][i])
      check[connection[x][i]]=1 ct>0 ct 0 ct-=1 ct 1 x=next.popleft() x next.popleft() next.popleft next popleft ans[x]=str(c[ct]) ans[x] ans x str(c[ct]) str c[ct] c ct for i in range(len(connection[x])):
    if check[connection[x][i]]==-1:
      next.append(connection[x][i])
      check[connection[x][i]]=1 i range(len(connection[x])) range len(connection[x]) len connection[x] connection x if check[connection[x][i]]==-1:
      next.append(connection[x][i])
      check[connection[x][i]]=1 check[connection[x][i]]==-1 check[connection[x][i]] check connection[x][i] [x] connection x i -1 next.append(connection[x][i]) next.append next append connection[x][i] [x] connection x i check[connection[x][i]]=1 check[connection[x][i]] check connection[x][i] [x] connection x i 1 print(sum(c)-c[n-1]) print sum(c)-c[n-1] sum(c) sum c c[n-1] c n-1 n 1 print(' '.join(ans)) print ' '.join(ans) ' '.join ' ' join ans l=[list(map(int,input().split())) for i in range(n-1)] [list(map(int,input().split())) for i in range(n-1)] l next=deque(next) deque(next) next c=list(map(int,input().split())) list(map(int,input().split())) c ct=n n ct next=[0] [0] next x=next.popleft() next.popleft() x n=int(input()) int(input()) n connection=[[] for i in range(n)] [[] for i in range(n)] connection ans=[0 for i in range(n)] [0 for i in range(n)] ans check=[-1 for i in range(n)] [-1 for i in range(n)] check ct-=1 1 ct
from collections import deque
n=int(input())
l=[list(map(int,input().split())) for i in range(n-1)]
c=list(map(int,input().split()))
c.sort()
connection=[[] for i in range(n)]
ans=[0 for i in range(n)]
for i in range(n-1):
  connection[l[i][0]-1].append(l[i][1]-1)
  connection[l[i][1]-1].append(l[i][0]-1)
next=[0]
next=deque(next)
check=[-1 for i in range(n)]
check[0]=1
ct=n
while ct>0:
  ct-=1
  x=next.popleft()
  ans[x]=str(c[ct])
  for i in range(len(connection[x])):
    if check[connection[x][i]]==-1:
      next.append(connection[x][i])
      check[connection[x][i]]=1
print(sum(c)-c[n-1])
print(' '.join(ans))