import           Control.Monad
import qualified Data.ByteString.Char8       as BS
import           Data.List
import           Data.Maybe
import qualified Data.Vector                 as VB
import qualified Data.Vector.Mutable         as VBM
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM


readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  n <- getInt
  t <- replicateM (n-1) getIntList

  vs <- VBM.new (n+1)
  VBM.set vs ([] :: [Int])

  forM_ t $ \[a, b] -> do
    la <- VBM.read vs a
    VBM.write vs a (b:la)
    lb <- VBM.read vs b
    VBM.write vs b (a:lb)

  vs' <- VB.freeze vs

  cs <- getIntList
  let cs' = sortBy (flip compare) cs

  ws <- VM.new (n+1)
  VM.set ws (0::Int)

  let bfs [] _ = return ()
      bfs (x:xs) cs = do
        w <- VM.read ws x
        if w == 0
          then do VM.write ws x (head cs)
                  let cs' = tail cs
                      xs' = xs ++ (vs' VB.! x)
                  bfs xs' cs'
          else bfs xs cs

  bfs [1] cs'

  print $ sum $ tail cs'

  forM_ [1..n] $ \i -> do
    when (i > 1) $ putStr " "
    w <- VM.read ws i
    (putStr . show) w

  putStrLn ""