module ddd;import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto G = new int[][](N);
    foreach (i; 0..N-1) {
        auto s = readln.split.map!(to!int);
        G[s[0]-1] ~= s[1]-1;
        G[s[1]-1] ~= s[0]-1;
    }
    auto A = readln.split.map!(to!long).array;
    A.sort!"a > b";

    auto ans = new long[](N);
    int cnt = 0;

    void dfs(int n, int p) {
        ans[n] = A[cnt++];
        foreach (m; G[n]) {
            if (m != p) {
                dfs(m, n);
            }
        }
    }

    dfs(0, -1);
    A[1..$].sum.writeln;
    ans.map!(to!string).join(" ").writeln;
}