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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_D main
#include "bits/stdc++.h" 
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i,s,e) for (int i = int(s); i < int(e); ++i)
#define REP(i,e) FOR(i,0,e)
#define INF (INT_MAX/2)
#define EPS (1.0e-8)
#define LINF (LLONG_MAX/2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long; using ull = unsigned long long;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vl = vector<ll>; using vvl = vector<vl>;
using vd = vector<double>; using vs = vector<string>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
// functions

#endif


int main_D() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vi A(N-1), B(N-1); 
    REP(i, N-1) {
        cin >> A[i] >> B[i];
        A[i]--; B[i]--;
    }
    A.push_back(N-1);
    B.push_back(N);

    vi C(N); REP(i, N) cin >> C[i];
    priority_queue<int> pqc;
    REP(i, N) pqc.push(C[i]);

    vvi es(N+1);
    REP(i,N) {
        es[A[i]].push_back(B[i]);
        es[B[i]].push_back(A[i]);
    }

    priority_queue<pii> pq;
    REP(i,N) pq.push(pii(es[i].size(), i));    

    map<int, int> ns;
    while(!pq.empty()) {
        pii p = pq.top(); pq.pop();

        if (pqc.empty()) break;

        if (ns.count(p.second)==0){
            ns[p.second] = pqc.top(); pqc.pop();
        }
        for(int i : es[p.second]) {
            if (i == N) continue;
            if (pqc.empty()) break;
            if (ns.count(i) == 0) {
                ns[i] = pqc.top(); pqc.pop();
            }
        }
    }

    map<pii, int> mp;
    REP(i,N) {
        mp[pii(A[i],B[i])] = min(ns[A[i]], ns[B[i]]);
        mp[pii(B[i], A[i])] = mp[pii(A[i], B[i])];
    }

    ll ans = 0;
    REP(i,N) {
        for (int j : es[i]) {
            ans += mp[pii(i, j)];
        }
    }
    ans /= 2;

    cout << ans << endl;
    cout << ns[0];
    FOR(i, 1, N) cout << " " << ns[i];
    cout << endl;

    return 0;
}