from collections import deque
n=int(input())
edge=[]
graph=[[] for i in range(n)]
point=[-1]*n
for i in range(n-1):
  a,b=map(int,input().split())
  a-=1;b-=1
  edge.append([a,b])
  graph[a].append(b)
  graph[b].append(a)
num=list(map(int,input().split()));num.sort()
start=0
for i in range(n):
  if len(graph[i])==1:
    start=i
    break
dfs=deque([start])
while dfs:
  v=dfs.popleft()
  point[v]=num.pop()
  for i in graph[v]:
    if point[i]!=-1:continue
    dfs.appendleft(i)
ans=0
for i in edge:
  ans+=min(point[i[0]],point[i[1]])
print(ans)
print(*point) from collections import deque n=int(input()) n int(input()) int input() input edge=[] edge [] [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] graph=[[] for i in range(n)] graph [[] for i in range(n)] point=[-1]*n point [-1]*n [-1] -1 n for i in range(n-1):
  a,b=map(int,input().split())
  a-=1;b-=1
  edge.append([a,b])
  graph[a].append(b)
  graph[b].append(a) i 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 edge.append([a,b]) edge.append edge append [a,b] a b graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a num=list(map(int,input().split())) num list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split num.sort() num.sort num sort start=0 start 0 for i in range(n):
  if len(graph[i])==1:
    start=i
    break i range(n) range n if len(graph[i])==1:
    start=i
    break len(graph[i])==1 len(graph[i]) len graph[i] graph i 1 start=i start i break dfs=deque([start]) dfs deque([start]) deque [start] start while dfs:
  v=dfs.popleft()
  point[v]=num.pop()
  for i in graph[v]:
    if point[i]!=-1:continue
    dfs.appendleft(i) dfs v=dfs.popleft() v dfs.popleft() dfs.popleft dfs popleft point[v]=num.pop() point[v] point v num.pop() num.pop num pop for i in graph[v]:
    if point[i]!=-1:continue
    dfs.appendleft(i) i graph[v] graph v if point[i]!=-1:continue
     point[i]!=-1 point[i] point i -1 dfs.appendleft(i) dfs.appendleft dfs appendleft i ans=0 ans 0 for i in edge:
  ans+=min(point[i[0]],point[i[1]]) i edge ans+=min(point[i[0]],point[i[1]]) ans min(point[i[0]],point[i[1]]) min point[i[0]] point i[0] i 0 point[i[1]] point i[1] i 1 print(ans) print ans print(*point) print *point point edge=[] [] edge n=int(input()) int(input()) n ans=0 0 ans num=list(map(int,input().split())) list(map(int,input().split())) num start=0 0 start a,b=map(int,input().split()) map(int,input().split()) a b=map(int,input().split()) map(int,input().split()) b dfs=deque([start]) deque([start]) dfs b-=1 1 b point=[-1]*n [-1]*n point graph=[[] for i in range(n)] [[] for i in range(n)] graph start=i i start ans+=min(point[i[0]],point[i[1]]) min(point[i[0]],point[i[1]]) ans v=dfs.popleft() dfs.popleft() v a-=1 1 a
from collections import deque
n=int(input())
edge=[]
graph=[[] for i in range(n)]
point=[-1]*n
for i in range(n-1):
  a,b=map(int,input().split())
  a-=1;b-=1
  edge.append([a,b])
  graph[a].append(b)
  graph[b].append(a)
num=list(map(int,input().split()));num.sort()
start=0
for i in range(n):
  if len(graph[i])==1:
    start=i
    break
dfs=deque([start])
while dfs:
  v=dfs.popleft()
  point[v]=num.pop()
  for i in graph[v]:
    if point[i]!=-1:continue
    dfs.appendleft(i)
ans=0
for i in edge:
  ans+=min(point[i[0]],point[i[1]])
print(ans)
print(*point)