#include <bits/stdc++.h>

using namespace std;
inline void
prep ()
{
  cin.tie (0);
  cin.sync_with_stdio (0);
};
//printf("%.12lf\n",mid);
// __builtin_popcount(int) count 1's in binary
int mod = 1000000007;

int mymod(int num){
  if (num< 0){
    if (mod == 1){
      return 0;
    }
    return mod-abs(num)%mod;
  }else{
    return num%mod;
  }
}

double eps = .0000000001;

int sublengths[100001];
vector<int> graph[10001];
int curn = 0;
bool realvis[10001];
int dfs1(int node, int par){
  if (realvis[node]){
    return 0;
  }
 
  int val = 1;
  for (int i=0; i<graph[node].size(); i++){
    if (graph[node][i] == par){
      continue;
    }
    val += dfs1(graph[node][i], node);
  }
  sublengths[node] = val;
  return val;
}
int centroid = 0;
void dfs2(int node, int passed, int par){
  if (realvis[node]){
    return;
  }
  if (passed > curn/2){
    return;
  }
  bool found = false;
  int go = 0;
  int thesum = 0;
  for (int i=0; i<graph[node].size(); i++){
    if (graph[node][i] != par && !realvis[graph[node][i]]){
      thesum += sublengths[graph[node][i]];
    }
  
  }
  for (int i=0; i<graph[node].size(); i++){
    if (graph[node][i] != par && sublengths[graph[node][i]] > curn/2 && !realvis[graph[node][i]]){
      found = true;
      go = graph[node][i];
      thesum -= sublengths[graph[node][i]];
      break;
    }
  
  }
  
  if (found){
    sublengths[node] = passed+thesum+1;
    dfs2(go, passed+thesum+1, node);
  }else{
    //cout << node << endl;
    centroid = node;
    return;
  }
  return;
}
int main ()
{
  prep ();
  int n;
  cin >> n;
  curn = n;
  vector<int> weights;
  for (int i=0; i<n-1; i++){
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  for (int i=0; i<n; i++){
        int num;
        cin >> num;
        weights.push_back(num);
  }
  sort(weights.rbegin(), weights.rend());
 
  bool visited[10001] = {false};
  queue<int> q;
  long long sum = 0;
  q.push(1);
  int thiss[10001] = {0};
  int curw = 0;
  while (!q.empty()){
      int front = q.front();
      q.pop();
      if (visited[front]){
          continue;
      }
      //cout << front << endl;
      visited[front] = true;
      
      if (front != 1){
          sum += weights[curw];
      }
      thiss[front] = weights[curw];
      curw++;
      for (int i=0; i<graph[front].size(); i++){
          q.push(graph[front][i]);
      }
  }
  cout << sum << endl;
  for (int i=1; i<=n ;i++){
      cout << thiss[i] << " ";
  }
  return 0;
}
// in dp problems think: up until here, intervals, or "what if there's 1, 2, 3..."
// in math problems write out equations
// also in math problems to change multiplication/division to addition/subtraction use logs, add/subtract something to make the multiplication easier (like if it's 3*f(x)+2, change it to 3*(f(x)+1)-1)
// if printint pow function, put an (int) or (long long) next to it
// if checked everything check for bugs (like sorting wrong way or not putting a break in a loop)
// if n is 10^7 try brute force if the operations are simple
// if can't do dp do brute force/greedy
// to save time don't copy maps/vectors into variables if it already exists
// only use long long if need to
// if wrong on decimal problem, check if program rounds or if you can round to a certain amt of decimals
// if decimals too, if can subtract instead of divide
// in grid traversal problems check if doing sideways or not
// if TLE try to take away loop
// in recursion looking math problems look at things from the point of view of both sides (the start and the end sections of the recursion)
// think about how many possible answers there are to the problem
// try to set the answer to the problem and see if it's possible (like w/ binary search)
// if it makes it easier use +1 indexing
// think twice when writing things down that you're going to use later
// for conversions think if you can compute them w/ only one conversion
// if the problem involves two things moving at the same time, see if you can generalise it to just one thing moving
// if nt think primes

Measured memory_bytes: 1024