Problem:
Score : 500 points

Problem Statement

You are given a tree with N vertices 1,2,\ldots,N, and positive integers c_1,c_2,\ldots,c_N.
The i-th edge in the tree (1 \leq i \leq N-1) connects Vertex a_i and Vertex b_i.

We will write a positive integer on each vertex in T and calculate our score as follows:

On each edge, write the smaller of the integers written on the two endpoints.

Let our score be the sum of the integers written on all the edges.

Find the maximum possible score when we write each of c_1,c_2,\ldots,c_N on one vertex in T, and show one way to achieve it. If an integer occurs multiple times in c_1,c_2,\ldots,c_N, we must use it that number of times.

Constraints

1 \leq N \leq 10000

1 \leq a_i,b_i \leq N

1 \leq c_i \leq 10^5

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}
c_1 \ldots c_N

Output

Use the following format:

M
d_1 \ldots d_N

where M is the maximum possible score, and d_i is the integer to write on Vertex i.
d_1,d_2,\ldots,d_N must be a permutation of c_1,c_2,\ldots,c_N.
If there are multiple ways to achieve the maximum score, any of them will be accepted.

Sample Input 1

5
1 2
2 3
3 4
4 5
1 2 3 4 5

Sample Output 1

10
1 2 3 4 5

If we write 1,2,3,4,5 on Vertex 1,2,3,4,5, respectively, the integers written on the four edges will be 1,2,3,4, for the score of 10. This is the maximum possible score.

Sample Input 2

5
1 2
1 3
1 4
1 5
3141 59 26 53 59

Sample Output 2

197
59 26 3141 59 53

c_1,c_2,\ldots,c_N may not be pairwise distinct.

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::*;

const SP: u8 = b' ';
const LF: u8 = b'\n';

struct CharacterInput<'a> {
    locked_stdin: StdinLock<'a>,
    bytes_buffer: Vec<u8>,
}

impl<'a> CharacterInput<'a> {
    fn read(&mut self, delimiter: u8) -> &[u8] {
        self.bytes_buffer.clear();

        let len = self
            .locked_stdin
            .read_until(delimiter, &mut self.bytes_buffer)
            .unwrap();

        if len > 0 {
            let len = len - 1;
            if self.bytes_buffer[len] == delimiter {
                self.bytes_buffer.truncate(len);
            }
        }

        &self.bytes_buffer
    }

    fn scan_u(&mut self, delimiter: u8) -> u32 {
        self.read(delimiter)
            .into_iter()
            .fold(0, |accumulator, byte| {
                (byte - b'0') as u32 + accumulator * 10
            })
    }

    fn scan_u_vec(&mut self, n: usize, separator: u8, delimiter: u8) -> Vec<u32> {
        let mut vector = Vec::with_capacity(n);
        for _i in 0..n - 1 {
            vector.push(self.scan_u(separator));
        }
        vector.push(self.scan_u(delimiter));
        vector
    }
}

fn create_cin<'a>(input: StdinLock<'a>, buffer_capacity: usize) -> CharacterInput {
    CharacterInput {
        locked_stdin: input,
        bytes_buffer: Vec::with_capacity(buffer_capacity),
    }
}

struct CharacterOutput {
    string_buffer: String,
}

impl CharacterOutput {
    fn push_s(&mut self, s: &str) {
        self.string_buffer += s;
    }

    fn push<T: ToString>(&mut self, n: T) {
        self.push_s(&n.to_string());
    }

    fn add<T: ToString>(&mut self, n: T, separator: &str) {
        self.push(n);
        self.push_s(separator);
    }

    fn flush(&mut self) {
        let out = stdout();
        let mut out = out.lock();
        out.write_all(self.string_buffer.as_bytes()).unwrap();
        self.string_buffer.clear();
    }
}

fn create_cout(capacity: usize) -> CharacterOutput {
    CharacterOutput {
        string_buffer: String::with_capacity(capacity),
    }
}

// ------------------------------------------------------------------------

struct Node {
    index: usize,
    weight: u32,
    adjacent_list: Vec<usize>,
}

type ScoredNode = (Node, u32);

fn get_score(node: ScoredNode) -> u32 {
    node.1
}

fn process(cin: &mut CharacterInput, cout: &mut CharacterOutput) {
    let n = cin.scan_u(LF) as usize;

    let mut node_list: Vec<Node> = Vec::with_capacity(n);
    for i in 0..n {
        node_list.push(Node {
            index: i,
            weight: 0,
            adjacent_list: Vec::with_capacity(n / 4),
        });
    }

    let mut edge_list: Vec<(usize, usize)> = Vec::with_capacity(n - 1);

    for _i in 0..n - 1 {
        let edge = (cin.scan_u(SP) as usize - 1, cin.scan_u(LF) as usize - 1);
        node_list[edge.0].weight += 1;
        node_list[edge.0].adjacent_list.push(edge.1);
        node_list[edge.1].weight += 1;
        node_list[edge.1].adjacent_list.push(edge.0);
        edge_list.push(edge);
    }

    // sort c
    let mut c = cin.scan_u_vec(n, SP, LF);
    c.sort_by(|c1, c2| c2.partial_cmp(c1).unwrap());

    // sort nodes by weight (todo: try to set the same c value if nodes have the same weight?)
    node_list.sort_by(|a, b| {
        if a.adjacent_list.contains(&b.index) {
            std::cmp::Ordering::Less
        } else {
            std::cmp::Ordering::Equal
        }
    });
    node_list.sort_by(|a, b| b.weight.partial_cmp(&a.weight).unwrap());

    // zip nodes and c, and sort by node index
    let mut scored_nodes: Vec<ScoredNode> = node_list.into_iter().zip(c.into_iter()).collect();
    scored_nodes.sort_by(|a, b| a.0.index.partial_cmp(&b.0.index).unwrap());

    // score
    let score = edge_list.into_iter().fold(0, |acc, edge| {
        acc + std::cmp::min(scored_nodes[edge.0].1, scored_nodes[edge.1].1)
    });

    // output
    cout.add(score, "\n");

    let mut iter = scored_nodes.into_iter();
    for _i in 0..n - 1 {
        cout.add(get_score(iter.next().unwrap()), " ");
    }
    cout.add(get_score(iter.next().unwrap()), "\n");
}

// ------------------------------------------------------------------------

fn main() {
    let cin = stdin();
    let cin = &mut create_cin(cin.lock(), 64);
    let cout = &mut create_cout(0);

    process(cin, cout);

    cout.flush();
}