import std.algorithm;
import std.array;
import std.container;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

void main() {
    int n = readint;
    int[][] edges;
    int[][] adjs;
    for (int i = 0; i <= n; i++) adjs ~= [[]];
    for (int i = 0; i < n-1; i++) {
        auto edge = readints;
        edges ~= edge;
        adjs[edge[0]] ~= edge[1];
        adjs[edge[1]] ~= edge[0];
    }
    int[] cs = readints;
    cs.sort();

    auto refs = new int[n + 1];
    foreach (e; edges) {
        refs[e[0]]++;
        refs[e[1]]++;
    }

    auto used = new int[n + 1];
    auto q = DList!int();
    for (int i = 1; i <= n; i++) {
        if (refs[i] == 1) {
            q.insertBack(i);
        }
    }

    int cp = 0;
    auto nodes = new int[n + 1]; // node score
    while (!q.empty) {
        int node = q.front;
        q.removeFront();
        if (used[node]++) continue;

        nodes[node] = cs[cp++];
        refs[node] = 0;
        foreach (a; adjs[node]) {
            if (used[a]) continue;

            refs[a]--;
            if (refs[a] == 1) {
                q.insertBack(a);
            }
        }
    }

    int score = 0;
    foreach (e; edges) {
        score += min(nodes[e[0]], nodes[e[1]]);
    }
    writeln(score);
    writeln(nodes.drop(1).map!(text).join(" "));




}

