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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Math;

class MainClass
{
    static void Main()
    {
        int N = int.Parse(ReadLine());
        var ab = new int[N][];
        var G = new Graph<int>(N);
        var degrees = new (int index,int cnt)[N];
        for (int i = 0; i < N; i++) degrees[i] = (i, 0);
        
        for (int i = 0; i < N-1; i++)
        {
            ab[i] = ReadLine().Split().Select(_=>int.Parse(_)-1).ToArray();
            int a = ab[i][0];
            int b = ab[i][1];
            G.Add(a, b);
            G.Add(b, a);
            degrees[a] = (a, degrees[a].cnt + 1);
            degrees[b] = (b, degrees[b].cnt + 1);
        }
        var c = ReadLine().Split().Select(long.Parse).ToArray();
        
        Array.Sort(c,(x,y)=>-x.CompareTo(y));
        Array.Sort(degrees, (x, y) => -x.cnt.CompareTo(y.cnt));

        var d = new long[N];
        var used = new bool[N];
        var cost = new long[N];
        d[degrees[0].index] = c[0];
        used[degrees[0].index] = true;
        foreach (var i in G[degrees[0].index])
        {
            cost[i] = c[0];
        }
        for (int i = 1; i < N; i++)
        {
            int maxcnt = 0;
            long maxcost = 0;
            int nindex = -1;//deg no
            for (int j = 0; j < N; j++)
            {
                var index = degrees[j].index;
                var cnt = degrees[j].cnt;
                if (used[index]) continue;
                if (maxcnt < cnt)
                {
                    maxcnt = cnt;
                    maxcost = cost[index];
                    nindex = index;
                }
                else if (maxcnt == cnt && cost[index] > maxcost)
                {
                    nindex = index;
                    maxcost = cost[index];
                }
            }
            d[nindex] = c[i];
            used[nindex] = true;
            foreach (var j in G[nindex])
            {
                cost[j] = Min(cost[j], c[i]);
            }
            
        }
        long ans = 0;
        for (int i = 0; i < N-1; i++)
        {
            ans += Min(d[ab[i][0]], d[ab[i][1]]);
        }

        WriteLine(ans);
        WriteLine(string.Join(" ",d));
    }
}
class Graph<T>
{
    private List<T>[] G;
    public Graph(int size)
    {
        G = new List<T>[size];
        for (int i = 0; i < size; i++)
        {
            G[i] = new List<T>();
        }
    }

    public List<T> this[int i] => G[i];
    public void Add(int from, T to) => G[from].Add(to);
    public List<T>[] ToListArray() => G;
    public int Length => G.Length;
}
public class PriorityQueue<T>
{
    public long Size { get; private set; } = 0;
    public long MaxSize { get; private set; } = 0;
    public T[] m_heap;
    private Comparison<T> Comp = null;

    public PriorityQueue(long maxSize, Comparison<T> comp)
    {
        if (maxSize <= 0)
            throw new Exception();
        MaxSize = maxSize;
        m_heap = new T[maxSize];

        Comp = comp;
    }

    public void Push(T x)
    {
        if (Size == MaxSize)
        {
            T[] new_heap = new T[MaxSize << 1];
            Array.Copy(m_heap, new_heap, MaxSize);
            m_heap = new_heap;
            MaxSize <<= 1;
        }

        long i = Size++;
        while (i > 0)
        {
            long p = (i - 1) / 2;
            if (Comp(m_heap[p], x) <= 0)
                break;
            m_heap[i] = m_heap[p];
            i = p;
        }
        m_heap[i] = x;
    }

    public T Pop()
    {
        if (Size == 0)
            throw new Exception("Queue is empty.");

        T result = m_heap[0];
        T x = m_heap[--Size];
        long i = 0;
        while (i * 2 + 1 < Size)
        {
            long c1 = i * 2 + 1, c2 = i * 2 + 2;
            if (c2 < Size && Comp(m_heap[c2], m_heap[c1]) < 0)
                c1 = c2;
            if (Comp(m_heap[c1], x) >= 0)
                break;
            m_heap[i] = m_heap[c1];
            i = c1;
        }
        m_heap[i] = x;

        return result;
    }
}