import collections
 
 
class Node(object):
 
  def __init__(self):
    self.value = 0
    self.edges = [] 
 
def main():
  n = int(input())
  nodes = [Node() for _ in range(n)]
 
  for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
  values = list(map(int, input().split()))
  values.sort(reverse=True)
 
  print(sum(values[1:]))
 
  queue = collections.deque()
  queue.append(nodes[0])
  index = 0
 
  while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
  print(' '.join(str(n.value) for n in nodes))
 
 
if __name__ == '__main__':
  main()
  import collections collections class Node(object):
 
  def __init__(self):
    self.value = 0
    self.edges = [] 
  Node def __init__(self):
    self.value = 0
    self.edges = [] 
  __init__ self.value = 0 self.value self value 0 self.edges = [] self.edges self edges [] self self object def main():
  n = int(input())
  nodes = [Node() for _ in range(n)]
 
  for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
  values = list(map(int, input().split()))
  values.sort(reverse=True)
 
  print(sum(values[1:]))
 
  queue = collections.deque()
  queue.append(nodes[0])
  index = 0
 
  while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
  print(' '.join(str(n.value) for n in nodes))
 
  main n = int(input()) n int(input()) int input() input Node() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) Node() Node nodes = [Node() for _ in range(n)] nodes [Node() for _ in range(n)] for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
   _ 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 nodes[a - 1].edges.append(nodes[b - 1]) [a - 1].edges.append [a - 1].edges [a - 1] nodes a - 1 a 1 edges append nodes[b - 1] nodes b - 1 b 1 nodes[b - 1].edges.append(nodes[a - 1]) [b - 1].edges.append [b - 1].edges [b - 1] nodes b - 1 b 1 edges append nodes[a - 1] nodes a - 1 a 1 values = list(map(int, input().split())) values list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split values.sort(reverse=True) values.sort values sort reverse=True reverse True print(sum(values[1:])) print sum(values[1:]) sum values[1:] values 1: 1 queue = collections.deque() queue collections.deque() collections.deque collections deque queue.append(nodes[0]) queue.append queue append nodes[0] nodes 0 index = 0 index 0 while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
   len(queue) != 0 len(queue) len queue 0 node = queue.popleft() node queue.popleft() queue.popleft queue popleft node.value = values[index] node.value node value values[index] values index index += 1 index 1 queue.extend(n for n in node.edges if n.value == 0) queue.extend queue extend n print(' '.join(str(n.value) for n in nodes)) print ' '.join(str(n.value) for n in nodes) ' '.join ' ' join str(n.value) str n.value n value if __name__ == '__main__':
  main()
  __name__ == '__main__' __name__ '__main__' main() main class Node(object):
 
  def __init__(self):
    self.value = 0
    self.edges = [] 
  class Node(object):
 
  def __init__(self):
    self.value = 0
    self.edges = [] 
  Node def main():
  n = int(input())
  nodes = [Node() for _ in range(n)]
 
  for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
  values = list(map(int, input().split()))
  values.sort(reverse=True)
 
  print(sum(values[1:]))
 
  queue = collections.deque()
  queue.append(nodes[0])
  index = 0
 
  while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
  print(' '.join(str(n.value) for n in nodes))
 
  def main():
  n = int(input())
  nodes = [Node() for _ in range(n)]
 
  for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
  values = list(map(int, input().split()))
  values.sort(reverse=True)
 
  print(sum(values[1:]))
 
  queue = collections.deque()
  queue.append(nodes[0])
  index = 0
 
  while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
  print(' '.join(str(n.value) for n in nodes))
 
  main
import collections
 
 
class Node(object):
 
  def __init__(self):
    self.value = 0
    self.edges = [] 
 
def main():
  n = int(input())
  nodes = [Node() for _ in range(n)]
 
  for _ in range(n - 1):
    a, b = map(int, input().split())
    nodes[a - 1].edges.append(nodes[b - 1])
    nodes[b - 1].edges.append(nodes[a - 1])
 
  values = list(map(int, input().split()))
  values.sort(reverse=True)
 
  print(sum(values[1:]))
 
  queue = collections.deque()
  queue.append(nodes[0])
  index = 0
 
  while len(queue) != 0:
    node = queue.popleft()
    node.value = values[index]
    index += 1
 
    queue.extend(n for n in node.edges if n.value == 0)
 
  print(' '.join(str(n.value) for n in nodes))
 
 
if __name__ == '__main__':
  main()
 