macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        let y = max!($($z),*);
        if $x > y {
            $x
        } else {
            y
        }
    }}
}
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        let y = min!($($z),*);
        if $x < y {
            $x
        } else {
            y
        }
    }}
}
use std::io::stdin;
use std::str::FromStr;
use std::io::Read;
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}
#[derive(Debug)]
pub struct Test {

}

impl Test {
    pub fn new(param: ()) -> Test {
        Test {
        }
    }

    fn solve(&mut self) -> usize {
        0
    }
}
fn main() {
    input! {
        n: usize,
        ab: [(usize1, usize1); n-1],
        c: [usize; n]
    };
    let mut c = c.to_vec();
    c.sort();

    let mut edge = vec![vec![]; n];
    let mut count = vec![0; n];
    for &v in &ab {
        edge[v.0].push(v.1);
        edge[v.1].push(v.0);
        count[v.0] += 1;
        count[v.1] += 1;
    }

    use std::collections::VecDeque;
    let mut queue = VecDeque::new();
    for i in 0..edge.len() {
        if count[i] ==  1 {
            queue.push_back(i);
        }
    }
    
    let mut res_edge = vec![0; n];
    let mut pos = 0;
    let mut sum = 0;
    while let Some(v) = queue.pop_front() {
        res_edge[v] = c[pos];
        pos += 1;
        if pos >= n { break; }
        sum += res_edge[v];

        for &e in &edge[v] {
            count[e] -= 1;
            if count[e] == 1 {
                queue.push_back(e);
            }
        }
    }

    println!("{}", sum);
    for res in res_edge {
        print!("{} ", res);
    }
    println!();
}


Measured memory_bytes: 4476