import           Control.Monad
import qualified Data.ByteString.Char8       as BS
import           Data.List
import           Data.Maybe
import qualified Data.Sequence               as Seq
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

  vs <- VBM.new (n+1)
  VBM.set vs (Seq.empty)

  replicateM (n-1) $ do
    [a, b] <- getIntList
    sa <- VBM.read vs a
    VBM.write vs a (sa Seq.|> b)
    sb <- VBM.read vs b
    VBM.write vs b (sb Seq.|> a)

  vs' <- VB.freeze vs

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

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

  let bfs s cs | Seq.null s = return ()
               | otherwise = do
                   let x = Seq.index s 0
                       xs = Seq.drop 1 s
                   w <- VM.read ws x
                   if w == 0
                     then do VM.write ws x (head cs)
                             let cs' = tail cs
                                 xs' = xs Seq.>< (vs' VB.! x)
                             bfs xs' cs'
                     else bfs xs cs

  bfs (Seq.fromList [1]) cs'

  print $ sum $ tail cs'

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

  putStrLn ""