import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math,
    std.functional, std.numeric, std.range, std.stdio, std.string, std.random,
    std.typecons, std.container, std.format;

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] aryread(T = long)(){return readln.split.to!(T[])();}
void scan(TList...)(ref TList Args){auto line = readln.split();
foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue = BinaryHeap!(Array!long, "a<b");
// dfmt on

void main()
{
    long N = lread();
    long[][long] G;
    foreach (_; 0 .. N - 1)
    {
        long a, b;
        scan(a, b);
        a--, b--;
        G[a] ~= b;
        G[b] ~= a;
    }
    auto C = aryread();
    C.sort!"b<a"();
    auto ans = new long[](N);
    auto Q = DList!long();
    Q.insertBack(0);
    foreach (i; 0 .. N)
    {
        long target = Q.front;
        Q.removeFront();
        ans[target] = C[i];
        foreach (child; G[target])
            if (ans[child] == 0)
                Q.insertBack(child);
    }
    C[1 .. $].sum().writeln();
    ans.map!text().join(' ').writeln();
}
