using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var N = consoleReader.Int;
            {
                var graphBuilder = new GraphBuilder(N);
                foreach (var l in consoleReader.Repeat(N - 1).SplitInt)
                {
                    graphBuilder.Add(l[0] - 1, l[1] - 1);
                }
                graph = graphBuilder.ToArray();
            }
            c = consoleReader.Split.Int.ToList();
            c.Sort();
            d = new int[N];
            int sum = c.Sum() - c[c.Count - 1];

            int index;
            for (index = 0; index < graph.Length; index++)
            {
                if (graph[index].Count == 1) break;
            }
            Next(index);

            return $"{sum}\n{string.Join(" ", d)}";
        }

        void Next(int index)
        {
            var lastCIndex = c.Count - 1;
            var last = c[lastCIndex];
            d[index] = last;
            c.RemoveAt(lastCIndex);
            for (var i = 0; i < graph[index].Count; i++)
            {
                var nextIndex = graph[index][i];
                graph[nextIndex].Remove(index);
                Next(nextIndex);
            }
        }

        List<int> c;
        int[] d;
        List<int>[] graph;

        public const int mod = 1000000007;
        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string MultiLine<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }
    class GraphBuilder
    {
        private List<int>[] lists;
        public GraphBuilder(int count)
        {
            this.lists = new List<int>[count];
            for (int i = 0; i < count; i++)
                this.lists[i] = new List<int>();
        }
        public void Add(int from, int to)
        {
            this.lists[from].Add(to);
            this.lists[to].Add(from);
        }
        public List<int>[] ToArray()
        {
            var N = this.lists.Length;
            var res = new List<int>[N];
            for (int i = 0; i < N; i++)
                res[i] = this.lists[i].ToList();
            return res;
        }
    }

#pragma warning disable
    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> Split { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}

Measured memory_bytes: 20028