#include<bits/stdc++.h>
#define EM 100000000
using namespace std;
using LL = long long;
using P = pair<LL, LL>;
LL LINF = 1e18;
int INF = 1e9;
using vint = vector<int>;
using vLL = vector<LL>;
using vvint = vector<vector<int>>;
using vvLL = vector<vector<LL>>;

int main(){
	int N;
	cin >> N;
	vvint g(N);
	for(int i = 0;i < N-1;i++){
		int ai, bi;
		cin >> ai >> bi;
		g[ai-1].push_back(bi-1);
		g[bi-1].push_back(ai-1);
	}
	vint c(N), used(N, 0), ans(N);
	for(int i = 0;i < N;i++)	cin >> c[i];
	sort(c.rbegin(), c.rend());
	LL num = 0;
	for(int i = 1;i < N;i++)	num += c[i];
	int id = 0;
	queue<int>	que;
	que.push(0);
	used[0] = 1;
	while(!que.empty()){
		int p = que.front();
		que.pop();
		ans[p] = c[id++];
		used[p] = 1;
		for(auto s : g[p]){
			if(!used[s])	que.push(s);
		}
	}
	cout << num << endl;
	for(auto s : ans)	cout << s << endl;
}