from heapq import heappush, heappop
N, *L = map(int, open(0).read().split())
dic = [[] for i in range(N)]
ans = [-1]*N
cnt = [0]*N
hq = []
for a,b in zip(*[iter(L[:2*(N-1)])]*2):
  a -= 1
  b -= 1
  dic[a] += [b]
  dic[b] += [a]
  cnt[a] += 1
  cnt[b] += 1
C = L[2*(N-1):]
C.sort()
for i in range(N):
  heappush(hq,(cnt[i],i))
arg = 0
m = sum(C)-C[-1]
while True:
  c, i = heappop(hq)
  if ans[i]!=-1:
    continue
  ans[i] = C[arg]
  arg += 1
  if arg==N:
    break
  for v in dic[i]:
    cnt[v] -= 1
    heappush(hq,(cnt[v],v))
print(m)
print(*ans) from heapq import heappush, heappop N, *L = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split *L L [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] dic = [[] for i in range(N)] dic [[] for i in range(N)] ans = [-1]*N ans [-1]*N [-1] -1 N cnt = [0]*N cnt [0]*N [0] 0 N hq = [] hq [] for a,b in zip(*[iter(L[:2*(N-1)])]*2):
  a -= 1
  b -= 1
  dic[a] += [b]
  dic[b] += [a]
  cnt[a] += 1
  cnt[b] += 1 a b zip(*[iter(L[:2*(N-1)])]*2) zip *[iter(L[:2*(N-1)])]*2 [iter(L[:2*(N-1)])]*2 [iter(L[:2*(N-1)])] iter(L[:2*(N-1)]) iter L[:2*(N-1)] L :2*(N-1) 2*(N-1) 2 N-1 N 1 2 a -= 1 a 1 b -= 1 b 1 dic[a] += [b] dic[a] dic a [b] b dic[b] += [a] dic[b] dic b [a] a cnt[a] += 1 cnt[a] cnt a 1 cnt[b] += 1 cnt[b] cnt b 1 C = L[2*(N-1):] C L[2*(N-1):] L 2*(N-1): 2*(N-1) 2 N-1 N 1 C.sort() C.sort C sort for i in range(N):
  heappush(hq,(cnt[i],i)) i range(N) range N heappush(hq,(cnt[i],i)) heappush hq (cnt[i],i) cnt[i] cnt i i arg = 0 arg 0 m = sum(C)-C[-1] m sum(C)-C[-1] sum(C) sum C C[-1] C -1 while True:
  c, i = heappop(hq)
  if ans[i]!=-1:
    continue
  ans[i] = C[arg]
  arg += 1
  if arg==N:
    break
  for v in dic[i]:
    cnt[v] -= 1
    heappush(hq,(cnt[v],v)) True c, i = heappop(hq) c heappop(hq) heappop hq i if ans[i]!=-1:
    continue
   ans[i]!=-1 ans[i] ans i -1 continue ans[i] = C[arg] ans[i] ans i C[arg] C arg arg += 1 arg 1 if arg==N:
    break
   arg==N arg N break for v in dic[i]:
    cnt[v] -= 1
    heappush(hq,(cnt[v],v)) v dic[i] dic i cnt[v] -= 1 cnt[v] cnt v 1 heappush(hq,(cnt[v],v)) heappush hq (cnt[v],v) cnt[v] cnt v v print(m) print m print(*ans) print *ans ans b -= 1 1 b m = sum(C)-C[-1] sum(C)-C[-1] m N, *L = map(int, open(0).read().split()) map(int, open(0).read().split()) N hq = [] [] hq ans = [-1]*N [-1]*N ans i = heappop(hq) heappop(hq) i cnt = [0]*N [0]*N cnt dic = [[] for i in range(N)] [[] for i in range(N)] dic c, i = heappop(hq) heappop(hq) c arg = 0 0 arg arg += 1 1 arg C = L[2*(N-1):] L[2*(N-1):] C a -= 1 1 a
from heapq import heappush, heappop
N, *L = map(int, open(0).read().split())
dic = [[] for i in range(N)]
ans = [-1]*N
cnt = [0]*N
hq = []
for a,b in zip(*[iter(L[:2*(N-1)])]*2):
  a -= 1
  b -= 1
  dic[a] += [b]
  dic[b] += [a]
  cnt[a] += 1
  cnt[b] += 1
C = L[2*(N-1):]
C.sort()
for i in range(N):
  heappush(hq,(cnt[i],i))
arg = 0
m = sum(C)-C[-1]
while True:
  c, i = heappop(hq)
  if ans[i]!=-1:
    continue
  ans[i] = C[arg]
  arg += 1
  if arg==N:
    break
  for v in dic[i]:
    cnt[v] -= 1
    heappush(hq,(cnt[v],v))
print(m)
print(*ans)