import scala.io.{StdIn => in}
import scala.collection.{mutable => mu}

object Main extends App {
  val n = in.readInt

  val edges = Iterator.continually(in.readLine)
    .take(n-1)
    .map(_.split(" ").map(_.toInt))
    .toArray

  val que = mu.PriorityQueue.empty[Int]
  in.readLine.split(" ").map(_.toInt)
    .foreach(que enqueue _)

  val graph = Array.fill(n+1)(mu.ListBuffer.empty[Int])
  edges.foreach { e =>
    e(1) +=: graph(e(0))
    e(0) +=: graph(e(1))
  }

  val node = Array.ofDim[Int](n+1)

  def dfs(first:Int):Unit = {
    val visited = Array.ofDim[Boolean](n+1)
    val stk = mu.Stack.empty[Int]
    stk push first

    while(stk.nonEmpty) {
      val v = stk.pop
      if(!visited(v)) {
        visited(v) = true
        node(v) = que.dequeue
        graph(v) foreach stk.push
      }
    }
  }

  dfs(1)

  val ans = (0 until n-1)./:(0){(acc, i) => 
    acc + ( node(edges(i)(0)) min node(edges(i)(1)) )
  }

  println(ans)
  println(node.tail.mkString(" "))
}