#include "bits/stdc++.h"
// Graph.h start--------------------------------------------
#pragma once

#include <vector>

using Weight = long long;

struct Vertex;
using Vertices = std::vector<Vertex *>;
struct Edge;
using Edges = std::vector<const Edge *>;

struct Vertex {
	const int id;
	Edges edges;
	Vertex(int id);
	void add_edge(const Edge *const p_e);
	void add_edge(const Edges &es);
};

struct Edge {
	Vertex *p_src, *p_dst;
	Weight weight;
	
	Edge(Vertex *p_src, Vertex *p_dst, Weight weight);
	Edge(const Edge &e);
	Edge& operator = (const Edge &de);
};

struct Graph {
	Vertices vertices;
	Edges edges;
	Graph(int nvertices);
	Graph();
	~Graph();
	void add_edge(Edge *p_edge);
	void add_edge(int edge_src, int edge_dst, Weight w);
};

//----------------------------------------------------------

using namespace std;

Vertex::Vertex(int id) : id(id) { }
void Vertex::add_edge(const Edge *const p_e) {
	edges.push_back(p_e);
}
void Vertex::add_edge(const Edges &es) {
	for (const auto &p_e : es) {
		add_edge(p_e);
	}
}

Edge::Edge(Vertex *p_src, Vertex *p_dst, Weight weight) : p_src(p_src), p_dst(p_dst), weight(weight) { }
Edge::Edge(const Edge &e) : Edge(e.p_src, e.p_dst, e.weight) {}
Edge& Edge::operator = (const Edge &e) {
	p_src = e.p_src;
	p_dst = e.p_dst;
	weight = e.weight;
	return *this;
}
bool operator < (const Edge &e1, const Edge &e2) {
	return e1.weight != e2.weight ? e1.weight > e2.weight : // !!INVERSE!!
		e1.p_src != e2.p_src ? e1.p_src->id < e2.p_src->id : e1.p_dst->id < e2.p_dst->id;
}

Graph::Graph(int nvertices) {
	for (int id = 0; id < nvertices; id++) {
		vertices.push_back(new Vertex(id));
	}
}
Graph::Graph() : Graph(0) {}
Graph::~Graph() {
	for (auto &p_vertex : vertices) {
		delete p_vertex;
	}
	for (auto &p_edge : edges) {
		delete p_edge;
	}
}
void Graph::add_edge(Edge *p_edge) {
	vertices[p_edge->p_src->id]->add_edge(p_edge);
	edges.push_back(p_edge);
}
void Graph::add_edge(int edge_src, int edge_dst, Weight w) {
	add_edge(new Edge(vertices[edge_src], vertices[edge_dst], w));
}

// Graph.h end----------------------------------------------

using namespace std;
using ll = long long int;
using pii = pair<int, int>;
using pil = pair<int, ll>;

int main() {
	int N;
	cin >> N;
	vector<int> a(N - 1), b(N - 1);
	for (int i = 0; i < N - 1; i++)
	{
		cin >> a[i] >> b[i];
	}
	vector<int> c(N + 1);
	for (int j = 1; j <= N; j++)
	{
		cin >> c[j];
	}

	Graph g(N + 1);
	for (int i = 0; i < N - 1; i++)
	{
		g.add_edge(a[i], b[i], 1);
		g.add_edge(b[i], a[i], 1);
	}

	queue<int> q;
	q.push(1);
	vector<bool> is_finished(N + 1, false);
	int cnt = 0;
	vector<int> d(N+1);
	sort(c.rbegin(), c.rend());
	d[1] = c[cnt];
	cnt++;
	is_finished[1] = true;
	while (!q.empty()) {
		queue<int> nq;
		while (!q.empty()) {
			int v = q.front();
			q.pop();
			for (const auto& pe : g.vertices[v]->edges)
			{
				int id_nv = pe->p_dst->id;
				if (!is_finished[id_nv]) {
					nq.push(id_nv);
					d[id_nv] = c[cnt];
					cnt++;
					is_finished[id_nv] = true;
				}
			}
		}
		q = nq;
	}

	int anssum = accumulate(c.begin(), c.end(), 0) - c[0];
	cout << anssum << endl;
	for (int i = 1; i <= N; i++)
	{
		cout << d[i] << " ";
	}
	cout << endl;

	return 0;
}

Measured memory_bytes: 2300