require "set"
class Node
  attr_accessor :i, :c
  attr_reader :linked_nodes
  def initialize(i)
    @i = i
    @c = nil
    @linked_nodes = Set.new
  end

  def add_link(node)
    @linked_nodes << node
  end

  def num_edge
    @linked_nodes.size
  end

  def set_c(c_ary)
    @c = c_ary.shift
    @linked_nodes.each do |node|
      next unless node.c.nil?
      node.set_c(c_ary)
    end
  end
end

class Edge
  def initialize(node1, node2)
    @node1 = node1
    @node2 = node2
    @node1.add_link(@node2)
    @node2.add_link(@node1)
  end

  def score
    [@node1.c, @node2.c].compact.min || 0
  end
end

class Tree
  def initialize
    @nodes = {}
    @edges = []
  end

  def add_node(i)
    @nodes[i] = Node.new(i)
  end

  def add_edge(i, j)
    @edges << Edge.new(@nodes[i], @nodes[j])
  end

  def solve(c_ary)
    max_num_edge_node = @nodes.values.sort_by(&:num_edge).last
    c_ary = c_ary.sort.reverse
    max_num_edge_node.set_c(c_ary)
    score = @edges.map(&:score).inject(&:+)
    puts score
    puts @nodes.values.map(&:c).join(" ")
  end
end

n = gets.chomp.to_i
tree = Tree.new
1.upto(n) { |i| tree.add_node(i) }

1.upto(n - 1) do
  i, j = gets.chomp.split(" ").map(&:to_i)
  tree.add_edge(i, j)
end
c_ary = gets.chomp.split(" ").map(&:to_i)
tree.solve(c_ary)
