N = int(input())
D = {i: [0] for i in range(N)}
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].append(b-1)
  D[a-1][0] += 1
  D[b-1].append(a-1)
  D[b-1][0] += 1
m = 0
nod = -1
for i in D:
  if m < D[i][0]:
    m = D[i][0]
    nod = i
c = list(map(int, input().split()))
c.sort(reverse=True)
pnt = 0
cnt = 1
p = [nod]
L = [-1 for _ in range(N)]
L[nod] = c[0]
while cnt < N:
  n = cnt
  for i in range(pnt, cnt):
    for j in range(1, D[p[i]][0]+1):
      if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
  pnt = n
S = 0
for i in D:
  for j in range(1, D[i][0]+1):
    S += min(L[i], L[D[i][j]])
print(S//2)
for i in range(N):
  print(L[i], end = ' '*(i != N - 1))
print() N = int(input()) N int(input()) int input() input i: [0] for i in range(N) for i in range(N) i range(N) range N for i in range(N) i [0] 0 D = {i: [0] for i in range(N)} D {i: [0] for i in range(N)} for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].append(b-1)
  D[a-1][0] += 1
  D[b-1].append(a-1)
  D[b-1][0] += 1 i range(N-1) range N-1 N 1 a, b = map(int, input().split()) a map(int, input().split()) map int input().split() ().split () input split b D[a-1].append(b-1) [a-1].append [a-1] D a-1 a 1 append b-1 b 1 D[a-1][0] += 1 D[a-1][0] [a-1] D a-1 a 1 0 1 D[b-1].append(a-1) [b-1].append [b-1] D b-1 b 1 append a-1 a 1 D[b-1][0] += 1 D[b-1][0] [b-1] D b-1 b 1 0 1 m = 0 m 0 nod = -1 nod -1 for i in D:
  if m < D[i][0]:
    m = D[i][0]
    nod = i i D if m < D[i][0]:
    m = D[i][0]
    nod = i m < D[i][0] m D[i][0] [i] D i 0 m = D[i][0] m D[i][0] [i] D i 0 nod = i nod i c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse=True) c.sort c sort reverse=True reverse True pnt = 0 pnt 0 cnt = 1 cnt 1 p = [nod] p [nod] nod -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 L = [-1 for _ in range(N)] L [-1 for _ in range(N)] L[nod] = c[0] L[nod] L nod c[0] c 0 while cnt < N:
  n = cnt
  for i in range(pnt, cnt):
    for j in range(1, D[p[i]][0]+1):
      if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
  pnt = n cnt < N cnt N n = cnt n cnt for i in range(pnt, cnt):
    for j in range(1, D[p[i]][0]+1):
      if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
   i range(pnt, cnt) range pnt cnt for j in range(1, D[p[i]][0]+1):
      if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
   j range(1, D[p[i]][0]+1) range 1 D[p[i]][0]+1 D[p[i]][0] [p[i]] D p[i] p i 0 1 if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
   L[D[p[i]][j]] == -1 L[D[p[i]][j]] L D[p[i]][j] [p[i]] D p[i] p i j -1 L[D[p[i]][j]] = c[cnt] L[D[p[i]][j]] L D[p[i]][j] [p[i]] D p[i] p i j c[cnt] c cnt cnt += 1 cnt 1 p.append(D[p[i]][j]) p.append p append D[p[i]][j] [p[i]] D p[i] p i j pnt = n pnt n S = 0 S 0 for i in D:
  for j in range(1, D[i][0]+1):
    S += min(L[i], L[D[i][j]]) i D for j in range(1, D[i][0]+1):
    S += min(L[i], L[D[i][j]]) j range(1, D[i][0]+1) range 1 D[i][0]+1 D[i][0] [i] D i 0 1 S += min(L[i], L[D[i][j]]) S min(L[i], L[D[i][j]]) min L[i] L i L[D[i][j]] L D[i][j] [i] D i j print(S//2) print S//2 S 2 for i in range(N):
  print(L[i], end = ' '*(i != N - 1)) i range(N) range N print(L[i], end = ' '*(i != N - 1)) print L[i] L i end = ' '*(i != N - 1) end ' '*(i != N - 1) ' ' i != N - 1 i N - 1 N 1 print() print n = cnt cnt n a, b = map(int, input().split()) map(int, input().split()) a pnt = 0 0 pnt p = [nod] [nod] p nod = -1 -1 nod L = [-1 for _ in range(N)] [-1 for _ in range(N)] L cnt = 1 1 cnt D = {i: [0] for i in range(N)} {i: [0] for i in range(N)} D cnt += 1 1 cnt c = list(map(int, input().split())) list(map(int, input().split())) c pnt = n n pnt S = 0 0 S m = 0 0 m m = D[i][0] D[i][0] m nod = i i nod S += min(L[i], L[D[i][j]]) min(L[i], L[D[i][j]]) S b = map(int, input().split()) map(int, input().split()) b N = int(input()) int(input()) N
N = int(input())
D = {i: [0] for i in range(N)}
for i in range(N-1):
  a, b = map(int, input().split())
  D[a-1].append(b-1)
  D[a-1][0] += 1
  D[b-1].append(a-1)
  D[b-1][0] += 1
m = 0
nod = -1
for i in D:
  if m < D[i][0]:
    m = D[i][0]
    nod = i
c = list(map(int, input().split()))
c.sort(reverse=True)
pnt = 0
cnt = 1
p = [nod]
L = [-1 for _ in range(N)]
L[nod] = c[0]
while cnt < N:
  n = cnt
  for i in range(pnt, cnt):
    for j in range(1, D[p[i]][0]+1):
      if L[D[p[i]][j]] == -1:
        L[D[p[i]][j]] = c[cnt]
        cnt += 1
        p.append(D[p[i]][j])
  pnt = n
S = 0
for i in D:
  for j in range(1, D[i][0]+1):
    S += min(L[i], L[D[i][j]])
print(S//2)
for i in range(N):
  print(L[i], end = ' '*(i != N - 1))
print()