import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import Data.List
import Control.Monad
import Data.Ord
import qualified Data.Sequence as Sq

main = do
    n <- readLn :: IO Int
    ab <- replicateM (n-1) $ do
        [a,b] <- map read . words <$> getLine
        return (a-1,b-1)
    cs <- sortOn Down . map read . words <$> getLine :: IO [Int]

    let edge = V.create $ do
            vec <- VM.replicate n []
            forM_ ab $ \(a,b) -> do
                VM.modify vec (a:) b
                VM.modify vec (b:) a
            return vec
    
    let ans = sum $ tail cs
    let sq = Sq.fromList cs
    let result = VU.create $ do
            vec <- VUM.replicate n (0 :: Int)
            let go Sq.Empty _ = return ()
                go ((par,this) Sq.:<| qs) (x Sq.:<| xs) = do
                    VUM.write vec this x
                    let qs' = foldl (Sq.|>) qs $ map (\x -> (this,x)) $ filter (/= par) (edge V.! this)
                    go qs' xs
            go (Sq.singleton (0,0)) sq
            return vec
    print ans
    VU.forM_ result print