import std;
import core.bitop;

// dfmt off
ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
void aryWrite(T = long)(T[] ary){ ary.map!(x => x.text()).join(' ').writeln(); }
alias Pair = Tuple!(long, "B", long, "W");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    auto n = lread();
    auto tree = new long[][](n, 0);
    foreach (_; iota(n - 1))
    {
        long a, b;
        scan(a, b);
        tree[a - 1] ~= b - 1;
        tree[b - 1] ~= a - 1;
    }
    auto c = aryread.sort!"a>b"().array();

    auto cost = new long[](n);
    void bfs()
    {
        auto q = DList!(long)(0L);
        auto reached = new bool[](n);
        reached[0] = true;
        long idx;

        while (!q.empty)
        {
            auto f = q.front();
            q.removeFront();
            cost[f] = c[idx++];

            foreach (e; tree[f])
            {
                if (!reached[e])
                {
                    q.insertBack(e);
                    reached[e] = true;
                }
            }
        }
    }

    bfs();
    c.reverse.take(n - 1).sum().writeln();
    cost.aryWrite();
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}
