module M = Map.Make(struct type t = int let compare = (-) end)
let () =
  Scanf.scanf "%d" @@ fun n ->
  let g = Array.init n (fun _ -> []) in
  for i = 1 to n-1 do
    Scanf.scanf " %d %d" @@ fun a b ->
    let a, b = a-1, b-1 in
    g.(a) <- b :: g.(a);
    g.(b) <- a :: g.(b);
  done;
  let c = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  Array.sort (fun x y -> y-x) c;

  let rec dfs x p (i, m) =
    let m = M.add x c.(i) m in
    List.fold_left (fun pr y ->
      if y = p || M.mem y m then pr
      else dfs y x pr
    ) (i+1, m) g.(x)
  in
  let _, m = dfs 0 (-1) (0, M.empty) in

  Printf.printf "%d\n" (Array.fold_left (+) (-c.(0)) c);
  m |> M.iter (fun k v -> Printf.printf "%d " v);
  print_newline ()
  
