fun main(args: Array<String>) {

    val n = readLine()!!.toInt()

    val nodes = mutableListOf<MutableList<Int>>()
    for (i in 0 until n) {
        nodes.add(mutableListOf())
    }

    for (i in 0 until n-1) {
        val (a, b) = readLine()!!.split(" ").map { it.toInt() }
        nodes[a-1].add(b-1)
        nodes[b-1].add(a-1)
    }

    val inputs = readLine()!!.split(" ").map { it.toInt() }.toMutableList()
    inputs.sortDescending()

    val iter = inputs.iterator()
    val attr = IntArray(n) {0}

    attr[0] = iter.next()

    var color = IntArray(n) {0}

    fun paint(now: Int) {
        color[now] = 1

        if (!iter.hasNext()){
            return
        }

        for(i in 0 until nodes[now].size){

            if(color[nodes[now][i]] == 1){
                continue
            }

            // 各ノードへ割当
            attr[nodes[now][i]] = iter.next()

            // 次のノードをペイント
            paint(nodes[now][i])
        }
    }

    var ans = 0L
    fun calc(now: Int) {
        color[now] = 1

        for(i in 0 until nodes[now].size){

            if(color[nodes[now][i]] == 1){
                continue
            }
            // スコアを計算
            ans+=attr[nodes[now][i]]

            // 次のノードで計算
            calc(nodes[now][i])
        }
    }

    paint(0)

    color = IntArray(n) {0}
    calc(0)

    println(ans)
    println(attr.toList().joinToString(" "))
}