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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using E = System.Linq.Enumerable;
class Program
{
    static long N;
    static long[] C;
    static Point[] Points;
    static bool[] used;
    static long ans = 0;
    static void Main(string[] args)
    {

        //A = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        N = long.Parse(Console.ReadLine());
        Points = new Point[N+1];
        used = new bool[N + 1];
        for (int i = 0; i <= N; i++)
        {
            Points[i] = new Point();
        }

        for (int i = 0; i < N - 1; i++)
        {
            var AB = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
            var A = AB[0];
            var B = AB[1];

            Points[A].adj.Add(B);
            Points[A].count++;
            Points[B].adj.Add(A);
            Points[B].count++;
        }

        C = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        Array.Sort(C);
        Array.Reverse(C);

        var sp = 0L;
        for (int i = 1; i <= N; i++)
        {
            if(Points[i].count > Points[sp].count)
            {
                sp = i;
            }
        }

        WFS(sp);
        used = new bool[N + 1];
        WFS2(sp);
        var ans2 = new long[N];
        for (int i = 0; i < N; i++)
        {
            ans2[i] = Points[i + 1].point;
        }
        Console.WriteLine(ans);
        Console.WriteLine(String.Join(" ",ans2));
    }

    static void WFS2(long stp)
    {
        var que = new Queue<long>();
        que.Enqueue(stp);
        used[stp] = true;

        while(que.Count > 0)
        {
            var top = que.Dequeue();
            foreach (var item in Points[top].adj)
            {
                if (!used[item])
                {
                    que.Enqueue(item);
                    ans += Math.Min(Points[item].point, Points[top].point);
                    used[item] = true;
                }
            }
        }
    }

    static void WFS(long stp)
    {
        var pq = new PriorityQueue<Tuple<long,long>>();
        var now = 0;
        pq.Enqueue(new Tuple<long, long>(Points[stp].count, stp));
        used[stp] = true;
        while (pq.Count() > 0)
        {
            var top = pq.Dequeue();
            Points[top.Item2].point = C[now];
            now++;
            foreach (var item in Points[top.Item2].adj)
            {
                if (!used[item])
                {
                    pq.Enqueue(new Tuple<long, long>(Points[item].count, item));
                    used[item] = true;
                }
            }
        }
    }
}
public class Point
{
    public List<long> adj;
    public long count = 0;
    public long point = 0;
    public Point()
    {
        adj = new List<long>();
    }
}

public class PriorityQueue<T> : IEnumerable<T>
{
    private readonly List<T> _data = new List<T>();
    private readonly IComparer<T> _comparer;
    private readonly bool _isDescending;

    public PriorityQueue(IComparer<T> comparer, bool isDescending = true)
    {
        _comparer = comparer;
        _isDescending = isDescending;
    }

    public PriorityQueue(Comparison<T> comparison, bool isDescending = true)
        : this(Comparer<T>.Create(comparison), isDescending)
    {
    }

    public PriorityQueue(bool isDescending = true)
        : this(Comparer<T>.Default, isDescending)
    {
    }

    public void Enqueue(T item)
    {
        _data.Add(item);
        var childIndex = _data.Count - 1;
        while (childIndex > 0)
        {
            var parentIndex = (childIndex - 1) / 2;
            if (Compare(_data[childIndex], _data[parentIndex]) >= 0)
                break;
            Swap(childIndex, parentIndex);
            childIndex = parentIndex;
        }
    }

    public T Dequeue()
    {
        var lastIndex = _data.Count - 1;
        var firstItem = _data[0];
        _data[0] = _data[lastIndex];
        _data.RemoveAt(lastIndex--);
        var parentIndex = 0;
        while (true)
        {
            var childIndex = parentIndex * 2 + 1;
            if (childIndex > lastIndex)
                break;
            var rightChild = childIndex + 1;
            if (rightChild <= lastIndex && Compare(_data[rightChild], _data[childIndex]) < 0)
                childIndex = rightChild;
            if (Compare(_data[parentIndex], _data[childIndex]) <= 0)
                break;
            Swap(parentIndex, childIndex);
            parentIndex = childIndex;
        }
        return firstItem;
    }

    public T Peek()
    {
        return _data[0];
    }

    private void Swap(int a, int b)
    {
        var tmp = _data[a];
        _data[a] = _data[b];
        _data[b] = tmp;
    }

    private int Compare(T a, T b)
    {
        return _isDescending ? _comparer.Compare(b, a) : _comparer.Compare(a, b);
    }

    public int Count => _data.Count;

    public IEnumerator<T> GetEnumerator()
    {
        return _data.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}