lines = []
while line = gets
    lines << line.chomp.split(' ').map(&:to_i)
end

N = lines[0][0]
C = 10 ** 6

degree = []
for i in 1..N do
  degree[i-1] = 0
end
for i in 1..N-1 do
  from = lines[i][0]
  to = lines[i][1]
  degree[from - 1] += 1
  degree[to - 1] += 1
end

deg_i = []
for i in 1..N do
  deg_i[i-1] = degree[i-1] * C + i
end

min_deg_id = deg_i.sort
min_id = []
for i in 1..N do
  min_id[i-1] = min_deg_id[i-1] % C
end

min_num = lines[N].sort

ans = []
for i in 1..N do
  ans[i-1] = 0
end
for i in 1..N do
  id = min_id[i-1]
  ans[id-1] = min_num[i-1]
end

def min(x,y)
  if x < y then
    return x
  else
    return y
  end
end

ans_num = 0
for i in 1..N-1 do
  from = lines[i][0]
  to = lines[i][1]
  ans_num += min(ans[from-1], ans[to-1])
end
puts ans_num
  
for i in 1..N do
  print ans[i-1].to_s
  if i != N then
    print " "
  end
end