local mce, mfl, msq, mmi, mma = math.ceil, math.floor, math.sqrt, math.min, math.max
local n = io.read("*n")
local at, bt, c = {}, {}
local c = {}
local ref = {}
for i = 1, n do
  ref[i] = {}
  ref[i].idx = i
  ref[i].v = 0
  ref[i].c = 0
end
for i = 1, n - 1 do
  local a, b = io.read("*n", "*n")
  at[i], bt[i] = a, b
  ref[a].v, ref[b].v = ref[a].v + 1, ref[b].v + 1
end
for i = 1, n do
  c[i] = io.read("*n")
end
table.sort(ref, function(x, y) return x.v < y.v end)
table.sort(c)
for i = 1, n do
  ref[i].c = c[i]
end
table.sort(ref, function(x, y) return x.idx < y.idx end)
local sum = 0
for i = 1, n - 1 do
  sum = sum + mmi(ref[at[i]].c, ref[bt[i]].c)
end
print(sum)
io.write(ref[1].c)
for i = 2, n do
  io.write(" " .. ref[i].c)
end
io.write("\n")
