from collections import deque

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.sort(reverse=True)

ans=0 
used=[0]*(N+1)
number=[0]*(N+1)
que=deque([1])

ccount=0
while que:
  v=que.popleft()
  if number[v]==0:
    number[v]=c[ccount]
    ccount+=1
  for e in G[v]:
    if used[e]:
      continue
    if number[e]==0:
      number[e]=c[ccount]
      ccount+=1
    ans+=min(number[v],number[e])
    que.append(e)
    used[v]=1
print(ans)
print(*number[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=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.sort(reverse=True) c.sort c sort reverse=True reverse True ans=0 ans 0 used=[0]*(N+1) used [0]*(N+1) [0] 0 N+1 N 1 number=[0]*(N+1) number [0]*(N+1) [0] 0 N+1 N 1 que=deque([1]) que deque([1]) deque [1] 1 ccount=0 ccount 0 while que:
  v=que.popleft()
  if number[v]==0:
    number[v]=c[ccount]
    ccount+=1
  for e in G[v]:
    if used[e]:
      continue
    if number[e]==0:
      number[e]=c[ccount]
      ccount+=1
    ans+=min(number[v],number[e])
    que.append(e)
    used[v]=1 que v=que.popleft() v que.popleft() que.popleft que popleft if number[v]==0:
    number[v]=c[ccount]
    ccount+=1
   number[v]==0 number[v] number v 0 number[v]=c[ccount] number[v] number v c[ccount] c ccount ccount+=1 ccount 1 for e in G[v]:
    if used[e]:
      continue
    if number[e]==0:
      number[e]=c[ccount]
      ccount+=1
    ans+=min(number[v],number[e])
    que.append(e)
    used[v]=1 e G[v] G v if used[e]:
      continue
     used[e] used e continue if number[e]==0:
      number[e]=c[ccount]
      ccount+=1
     number[e]==0 number[e] number e 0 number[e]=c[ccount] number[e] number e c[ccount] c ccount ccount+=1 ccount 1 ans+=min(number[v],number[e]) ans min(number[v],number[e]) min number[v] number v number[e] number e que.append(e) que.append que append e used[v]=1 used[v] used v 1 print(ans) print ans print(*number[1:]) print *number[1:] number[1:] number 1: 1 v=que.popleft() que.popleft() v ccount+=1 1 ccount b=map(int,input().split()) map(int,input().split()) b ccount=0 0 ccount a,b=map(int,input().split()) map(int,input().split()) a ans=0 0 ans G=[[] for _ in range(N+1)] [[] for _ in range(N+1)] G used=[0]*(N+1) [0]*(N+1) used N=int(input()) int(input()) N ccount+=1 1 ccount c=list(map(int,input().split())) list(map(int,input().split())) c ans+=min(number[v],number[e]) min(number[v],number[e]) ans que=deque([1]) deque([1]) que number=[0]*(N+1) [0]*(N+1) number
from collections import deque

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.sort(reverse=True)

ans=0 
used=[0]*(N+1)
number=[0]*(N+1)
que=deque([1])

ccount=0
while que:
  v=que.popleft()
  if number[v]==0:
    number[v]=c[ccount]
    ccount+=1
  for e in G[v]:
    if used[e]:
      continue
    if number[e]==0:
      number[e]=c[ccount]
      ccount+=1
    ans+=min(number[v],number[e])
    que.append(e)
    used[v]=1
print(ans)
print(*number[1:])