from collections import deque
n=int(input())
d=dict()
edge=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  edge[a].append(b)
  edge[b].append(a)
c=list(map(int,input().split()))
c.sort(reverse=True)
visited=[-1]*n
def bfs(s):
  i=0
  q=deque([s])
  visited[s]=c[i]
  while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
  return
bfs(0)
print(sum(c)-max(c))
print(*visited) from collections import deque n=int(input()) n int(input()) int input() input d=dict() d dict() dict [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] edge=[[] for _ in range(n)] edge [[] for _ in range(n)] for _ in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  edge[a].append(b)
  edge[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 a-=1 a 1 b-=1 b 1 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge 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.sort(reverse=True) c.sort c sort reverse=True reverse True visited=[-1]*n visited [-1]*n [-1] -1 n def bfs(s):
  i=0
  q=deque([s])
  visited[s]=c[i]
  while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
  return bfs i=0 i 0 q=deque([s]) q deque([s]) deque [s] s visited[s]=c[i] visited[s] visited s c[i] c i while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
   q x=q.popleft() x q.popleft() q.popleft q popleft for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
   nx edge[x] edge x if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
   visited[nx]==-1 visited[nx] visited nx -1 i+=1 i 1 visited[nx]=c[i] visited[nx] visited nx c[i] c i q.append(nx) q.append q append nx return s s bfs(0) bfs 0 print(sum(c)-max(c)) print sum(c)-max(c) sum(c) sum c max(c) max c print(*visited) print *visited visited n=int(input()) int(input()) n def bfs(s):
  i=0
  q=deque([s])
  visited[s]=c[i]
  while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
  return def bfs(s):
  i=0
  q=deque([s])
  visited[s]=c[i]
  while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
  return bfs b=map(int,input().split()) map(int,input().split()) b b-=1 1 b c=list(map(int,input().split())) list(map(int,input().split())) c a-=1 1 a visited=[-1]*n [-1]*n visited d=dict() dict() d a,b=map(int,input().split()) map(int,input().split()) a edge=[[] for _ in range(n)] [[] for _ in range(n)] edge
from collections import deque
n=int(input())
d=dict()
edge=[[] for _ in range(n)]
for _ in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  edge[a].append(b)
  edge[b].append(a)
c=list(map(int,input().split()))
c.sort(reverse=True)
visited=[-1]*n
def bfs(s):
  i=0
  q=deque([s])
  visited[s]=c[i]
  while q:
    x=q.popleft()
    for nx in edge[x]:
      if visited[nx]==-1:
        i+=1
        visited[nx]=c[i]
        q.append(nx)
  return
bfs(0)
print(sum(c)-max(c))
print(*visited)