import collections

n=int(input())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
c=list(map(int,input().split()))
c=sorted(c,reverse=True)
m=sum(c)-c[0]
ans=[0]*(n+1)
ans[1]=c[0]
q=collections.deque()
q.append(1)
checked=[0]*(n+1)
checked[1]=1
cnt=1
while len(q):
  tmp=q.popleft()
  for v in g[tmp]:
    if checked[v]==0:
      checked[v]=1
      q.append(v)
      ans[v]=c[cnt]
      cnt+=1
print(m)
print(*ans[1:]) import collections collections 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=map(int,input().split())
  g[a].append(b)
  g[b].append(a) _ 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 g[a].append(b) [a].append [a] g a append b g[b].append(a) [b].append [b] g b append a 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=sorted(c,reverse=True) c sorted(c,reverse=True) sorted c reverse=True reverse True m=sum(c)-c[0] m sum(c)-c[0] sum(c) sum c c[0] c 0 ans=[0]*(n+1) ans [0]*(n+1) [0] 0 n+1 n 1 ans[1]=c[0] ans[1] ans 1 c[0] c 0 q=collections.deque() q collections.deque() collections.deque collections deque q.append(1) q.append q append 1 checked=[0]*(n+1) checked [0]*(n+1) [0] 0 n+1 n 1 checked[1]=1 checked[1] checked 1 1 cnt=1 cnt 1 while len(q):
  tmp=q.popleft()
  for v in g[tmp]:
    if checked[v]==0:
      checked[v]=1
      q.append(v)
      ans[v]=c[cnt]
      cnt+=1 len(q) len q tmp=q.popleft() tmp q.popleft() q.popleft q popleft for v in g[tmp]:
    if checked[v]==0:
      checked[v]=1
      q.append(v)
      ans[v]=c[cnt]
      cnt+=1 v g[tmp] g tmp if checked[v]==0:
      checked[v]=1
      q.append(v)
      ans[v]=c[cnt]
      cnt+=1 checked[v]==0 checked[v] checked v 0 checked[v]=1 checked[v] checked v 1 q.append(v) q.append q append v ans[v]=c[cnt] ans[v] ans v c[cnt] c cnt cnt+=1 cnt 1 print(m) print m print(*ans[1:]) print *ans[1:] ans[1:] ans 1: 1 c=sorted(c,reverse=True) sorted(c,reverse=True) c m=sum(c)-c[0] sum(c)-c[0] m ans=[0]*(n+1) [0]*(n+1) ans n=int(input()) int(input()) n cnt+=1 1 cnt a,b=map(int,input().split()) map(int,input().split()) a cnt=1 1 cnt tmp=q.popleft() q.popleft() tmp checked=[0]*(n+1) [0]*(n+1) checked g=[[] for _ in range(n+1)] [[] for _ in range(n+1)] g q=collections.deque() collections.deque() q b=map(int,input().split()) map(int,input().split()) b c=list(map(int,input().split())) list(map(int,input().split())) c
import collections

n=int(input())
g=[[] for _ in range(n+1)]
for _ in range(n-1):
  a,b=map(int,input().split())
  g[a].append(b)
  g[b].append(a)
c=list(map(int,input().split()))
c=sorted(c,reverse=True)
m=sum(c)-c[0]
ans=[0]*(n+1)
ans[1]=c[0]
q=collections.deque()
q.append(1)
checked=[0]*(n+1)
checked[1]=1
cnt=1
while len(q):
  tmp=q.popleft()
  for v in g[tmp]:
    if checked[v]==0:
      checked[v]=1
      q.append(v)
      ans[v]=c[cnt]
      cnt+=1
print(m)
print(*ans[1:])