n = int(input())
if n==1:
  c=int(input())
  print(c)
  print(c)
  quit()
line = [list(map(int, input().split())) for i in range(n-1)]
c = list(map(int, input().split()))
c.sort(reverse=True)
print(sum(c)-c[0])
ans=[0]*n
vis=[True]*n
a, b = line[0]
a-=1
b-=1
ans[a]=c[0]
ans[b]=c[1]
vis[a]=False
vis[b]=False
for i in range(1, n-1):
  for l in line:
    a, b = l
    a-=1
    b-=1
    if (vis[a] and (not vis[b])):
      ans[a]=c[i+1]
      vis[a]=False
      break
    elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans)
ans = list(map(str, ans))
print(" ".join(ans)) n = int(input()) n int(input()) int input() input if n==1:
  c=int(input())
  print(c)
  print(c)
  quit() n==1 n 1 c=int(input()) c int(input()) int input() input print(c) print c print(c) print c quit() quit 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 line = [list(map(int, input().split())) for i in range(n-1)] line [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(reverse=True) c.sort c sort reverse=True reverse True print(sum(c)-c[0]) print sum(c)-c[0] sum(c) sum c c[0] c 0 ans=[0]*n ans [0]*n [0] 0 n vis=[True]*n vis [True]*n [True] True n a, b = line[0] a line[0] line 0 b a-=1 a 1 b-=1 b 1 ans[a]=c[0] ans[a] ans a c[0] c 0 ans[b]=c[1] ans[b] ans b c[1] c 1 vis[a]=False vis[a] vis a False vis[b]=False vis[b] vis b False for i in range(1, n-1):
  for l in line:
    a, b = l
    a-=1
    b-=1
    if (vis[a] and (not vis[b])):
      ans[a]=c[i+1]
      vis[a]=False
      break
    elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans) i range(1, n-1) range 1 n-1 n 1 for l in line:
    a, b = l
    a-=1
    b-=1
    if (vis[a] and (not vis[b])):
      ans[a]=c[i+1]
      vis[a]=False
      break
    elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans) l line a, b = l a l b a-=1 a 1 b-=1 b 1 if (vis[a] and (not vis[b])):
      ans[a]=c[i+1]
      vis[a]=False
      break
    elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans) vis[a] and (not vis[b]) vis[a] vis a not vis[b] vis[b] vis b ans[a]=c[i+1] ans[a] ans a c[i+1] c i+1 i 1 vis[a]=False vis[a] vis a False break elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans) vis[b] and (not vis[a]) vis[b] vis b not vis[a] vis[a] vis a ans[b]=c[i+1] ans[b] ans b c[i+1] c i+1 i 1 vis[b]=False vis[b] vis b False break ans = list(map(str, ans)) ans list(map(str, ans)) list map(str, ans) map str ans print(" ".join(ans)) print " ".join(ans) " ".join " " join ans b-=1 1 b a, b = line[0] line[0] a b = line[0] line[0] b a-=1 1 a b = l l b a, b = l l a n = int(input()) int(input()) n c = list(map(int, input().split())) list(map(int, input().split())) c ans=[0]*n [0]*n ans line = [list(map(int, input().split())) for i in range(n-1)] [list(map(int, input().split())) for i in range(n-1)] line b-=1 1 b ans = list(map(str, ans)) list(map(str, ans)) ans vis=[True]*n [True]*n vis c=int(input()) int(input()) c a-=1 1 a
n = int(input())
if n==1:
  c=int(input())
  print(c)
  print(c)
  quit()
line = [list(map(int, input().split())) for i in range(n-1)]
c = list(map(int, input().split()))
c.sort(reverse=True)
print(sum(c)-c[0])
ans=[0]*n
vis=[True]*n
a, b = line[0]
a-=1
b-=1
ans[a]=c[0]
ans[b]=c[1]
vis[a]=False
vis[b]=False
for i in range(1, n-1):
  for l in line:
    a, b = l
    a-=1
    b-=1
    if (vis[a] and (not vis[b])):
      ans[a]=c[i+1]
      vis[a]=False
      break
    elif (vis[b] and (not vis[a])):
      ans[b]=c[i+1]
      vis[b]=False
      break
#print(ans)
ans = list(map(str, ans))
print(" ".join(ans))