//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>

#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

#include <stdexcept>

using namespace std;

//conversion
//------------------------------------------
inline int toint(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (l" << __line__ << ")" << " " << __file__ << endl

// type alias
using ll = long long;
using ull = unsigned long long;

template<class ll>
inline ll getCeilExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	--a;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

template<class ll>
inline ll getFloorExponent(ll a)
{
	if (a <= 0)
	{
		throw invalid_argument("vaule equal to or less than 0 should not be valid");
	}

	ll index = 0;
	a >>= 1;
	for (; a != 0; a >>= 1)
	{
		++index;
	}
	return index;
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline ll getGcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return getGcd(b, a % b);
}

// computational complexity: o(log(max(a, b))) 
template<class ll>
inline pair<ll, ll> getBezoutsIdentitySolution(ll a, ll b)
{
	if (b == 0)
	{
		return { 1, 0 };
	}
	auto sol = getBezoutsIdentitySolution(b, a % b);
	return { sol.second, sol.first - (a / b) * sol.second };
}

template<class Integer>
inline Integer getPower(Integer base, unsigned exponential)
{
	Integer res = 1;
	while (exponential >= 1)
	{
		if (exponential & 1)
		{
			res *= base;
		}
		base = base * base;
		exponential >>= 1;
	}

	return res;
}

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

	ll N;
	cin >> N;

	vector<vector<ll>> edges(N + 1);
	ll a, b;
	for (ll i = 0; i < N - 1; ++i)
	{
		cin >> a >> b;
		edges[a].emplace_back(b);
		edges[b].emplace_back(a);
	}

	vector<ll> sizes(N + 1);
	for (ll i = 1; i <= N; ++i)
	{
		sizes[i] = edges[i].size();
	}

	auto max_ = max_element(edges.begin() + 1, edges.end(), [](const auto& a, const auto& b) { return a.size() < b.size(); })->size();
	vector<set<ll>> sets(max_ + 1);
	for (ll i = 1; i <= N; ++i)
	{
		sets[edges[i].size()].insert(i);
	}

	vector<ll> cs(N);
	for (ll i = 0; i < N; ++i)
	{
		cin >> cs[i];
	}
	sort(cs.begin(), cs.end());

	vector<ll> rewrites(N + 1);
	ll min_count = 0;
	for (ll i = 0; i < N; ++i)
	{
		while (sets[min_count].size() == 0)
		{
			++min_count;
		}
		auto pushed = *(sets[min_count].begin());
 		rewrites[pushed] = cs[i];
		sets[min_count].erase(pushed);
		for (ll i = 0; i < edges[pushed].size(); ++i)
		{
			auto tmp = edges[pushed][i];
			if (sets[sizes[tmp]].erase(tmp) >= 1)
			{
				sets[sizes[tmp] - 1].insert(tmp);
				--sizes[tmp];
			}
		}
		--min_count;
	}

	ll res = 0;
	for (ll i = 1; i <= N; ++i)
	{
		for (ll j = 0; j < edges[i].size(); ++j)
		{
			res += min(rewrites[i], rewrites[edges[i][j]]);
		}
	}
	cout << res / 2 << endl;

	for (ll i = 1; i <= N; ++i)
	{
		cout << rewrites[i];
		if (i != N)
		{
			cout << " ";
		}
	}
	cout << endl;


	return 0;
}


Measured memory_bytes: 2176