local n = io.read("*n")
local edge = {}
local edgecnt = {}
local cost = {}
for i = 1, n do
  edge[i] = {}
  edgecnt[i] = 0
  cost[i] = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  edgecnt[a] = edgecnt[a] + 1
  edgecnt[b] = edgecnt[b] + 1
  edge[a][b], edge[b][a] = true, true
end
local c = {}
for i = 1, n do
  c[i] = io.read("*n")
end
table.sort(c)
do
  local csum = 0
  for i = 1, n - 1 do
    csum = csum + c[i]
  end
  print(csum)
end
local tasks = {}
for i = 1, n do
  if edgecnt[i] == 1 then
    table.insert(tasks, i)
  end
end
for i = 1, n do
  local src = tasks[i]
  cost[src] = c[i]
  for dst, _u in pairs(edge[src]) do
    if 1 < edgecnt[dst] then
      edgecnt[dst] = edgecnt[dst] - 1
      if edgecnt[dst] == 1 then
        table.insert(tasks, dst)
      end
    end
  end
end
print(table.concat(cost, " "))
