import sys 
sys.setrecursionlimit(10**6)
N=int(input())
A=[[]for i in range(N)]
X=[list(map(int, input().split())) for _ in range(N-1)]
for i in range(N-1):
  a=X[i][0]
  b=X[i][1]
  a-=1
  b-=1
  A[a].append(b)
  A[b].append(a)
D=list(map(int, input().split()))
D=sorted(D)[::-1]
from collections import deque
d=deque(D)
E=[-1]*N
def f(now,pre):
  for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now)
E[0]=d.popleft()
f(0,0)
print(sum(D[1:]))
print(*E) import sys sys sys.setrecursionlimit(10**6) sys.setrecursionlimit sys setrecursionlimit 10**6 10 6 N=int(input()) N int(input()) int input() input []for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] A=[[]for i in range(N)] A [[]for i in range(N)] list(map(int, input().split())) for _ in range(N-1) for _ in range(N-1) _ range(N-1) range N-1 N 1 for _ in range(N-1) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split X=[list(map(int, input().split())) for _ in range(N-1)] X [list(map(int, input().split())) for _ in range(N-1)] for i in range(N-1):
  a=X[i][0]
  b=X[i][1]
  a-=1
  b-=1
  A[a].append(b)
  A[b].append(a) i range(N-1) range N-1 N 1 a=X[i][0] a X[i][0] [i] X i 0 b=X[i][1] b X[i][1] [i] X i 1 a-=1 a 1 b-=1 b 1 A[a].append(b) [a].append [a] A a append b A[b].append(a) [b].append [b] A b append a D=list(map(int, input().split())) D list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split D=sorted(D)[::-1] D sorted(D)[::-1] (D) sorted D ::-1 -1 from collections import deque d=deque(D) d deque(D) deque D E=[-1]*N E [-1]*N [-1] -1 N def f(now,pre):
  for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now) f for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now) i A[now] A now if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now) i!=pre and E[i]==-1 i!=pre i pre E[i]==-1 E[i] E i -1 E[i]=d.popleft() E[i] E i d.popleft() d.popleft d popleft f(i,now) f i now now now pre pre E[0]=d.popleft() E[0] E 0 d.popleft() d.popleft d popleft f(0,0) f 0 0 print(sum(D[1:])) print sum(D[1:]) sum D[1:] D 1: 1 print(*E) print *E E d=deque(D) deque(D) d X=[list(map(int, input().split())) for _ in range(N-1)] [list(map(int, input().split())) for _ in range(N-1)] X a-=1 1 a E=[-1]*N [-1]*N E def f(now,pre):
  for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now) def f(now,pre):
  for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now) f N=int(input()) int(input()) N b=X[i][1] X[i][1] b b-=1 1 b D=list(map(int, input().split())) list(map(int, input().split())) D D=sorted(D)[::-1] sorted(D)[::-1] D a=X[i][0] X[i][0] a A=[[]for i in range(N)] [[]for i in range(N)] A
import sys 
sys.setrecursionlimit(10**6)
N=int(input())
A=[[]for i in range(N)]
X=[list(map(int, input().split())) for _ in range(N-1)]
for i in range(N-1):
  a=X[i][0]
  b=X[i][1]
  a-=1
  b-=1
  A[a].append(b)
  A[b].append(a)
D=list(map(int, input().split()))
D=sorted(D)[::-1]
from collections import deque
d=deque(D)
E=[-1]*N
def f(now,pre):
  for i in A[now]:
    if i!=pre and E[i]==-1:
      E[i]=d.popleft()
      f(i,now)
E[0]=d.popleft()
f(0,0)
print(sum(D[1:]))
print(*E)
