let n = Scanf.scanf " %d" (+) 0
let g = Array.make n []
let cs = Array.make n 0
let ans = Array.make n 0
let ci = ref 0
let rec f v p =
  ans.(v) <- cs.(!ci); incr ci;
  List.iter (fun u -> if u <> p then f u v) g.(v)
let _ =
  for _ = 1 to n - 1 do
    Scanf.scanf " %d %d" @@ fun u v ->
      g.(u - 1) <- v - 1 :: g.(u - 1); g.(v - 1) <- u - 1 :: g.(v - 1) done;
  for i = 0 to n - 1 do Scanf.scanf " %d" @@ fun c -> cs.(i) <- c done;
  Array.sort (fun x y -> y - x) cs;
  f 0 @@ -1;
  Array.fold_left (+) 0 cs - Array.fold_left max 0 cs |> Printf.printf "%d\n";
  ans |> Array.iter @@ Printf.printf "%d\n"