from collections import deque
N = int( input())
E = [[] for _ in range(N)]
V = [0]*N
for _ in range(N-1):
    a, b = map( int, input().split())
    a, b = a-1, b-1
    E[a].append(b)
    E[b].append(a)
    V[a] += 1
    V[b] += 1
C = list(map( int, input().split()))
C.sort(reverse=True)
for i in range(N):
    if V[i] == 1:
        s = i
        break
q = deque([s])
V = [0]*N
l = 0
for l in range(N):
    t = q.popleft()
    V[t] = C[l]
    for v in E[t]:
        if V[v] == 0:
            q.append(v)
print( sum(C) - C[0])
print( " ".join( map( str, V))) from collections import deque N = int( input()) N int( input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] E = [[] for _ in range(N)] E [[] for _ in range(N)] V = [0]*N V [0]*N [0] 0 N for _ in range(N-1):
    a, b = map( int, input().split())
    a, b = a-1, b-1
    E[a].append(b)
    E[b].append(a)
    V[a] += 1
    V[b] += 1 _ 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 a, b = a-1, b-1 a a-1 a 1 b b-1 b 1 E[a].append(b) [a].append [a] E a append b E[b].append(a) [b].append [b] E b append a V[a] += 1 V[a] V a 1 V[b] += 1 V[b] V b 1 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 for i in range(N):
    if V[i] == 1:
        s = i
        break i range(N) range N if V[i] == 1:
        s = i
        break V[i] == 1 V[i] V i 1 s = i s i break q = deque([s]) q deque([s]) deque [s] s V = [0]*N V [0]*N [0] 0 N l = 0 l 0 for l in range(N):
    t = q.popleft()
    V[t] = C[l]
    for v in E[t]:
        if V[v] == 0:
            q.append(v) l range(N) range N t = q.popleft() t q.popleft() q.popleft q popleft V[t] = C[l] V[t] V t C[l] C l for v in E[t]:
        if V[v] == 0:
            q.append(v) v E[t] E t if V[v] == 0:
            q.append(v) V[v] == 0 V[v] V v 0 q.append(v) q.append q append v print( sum(C) - C[0]) print sum(C) - C[0] sum(C) sum C C[0] C 0 print( " ".join( map( str, V))) print " ".join( map( str, V)) " ".join " " join map( str, V) map str V C = list(map( int, input().split())) list(map( int, input().split())) C b = map( int, input().split()) map( int, input().split()) b N = int( input()) int( input()) N a, b = a-1, b-1 a-1 a l = 0 0 l b = a-1, b-1 b-1 b E = [[] for _ in range(N)] [[] for _ in range(N)] E q = deque([s]) deque([s]) q t = q.popleft() q.popleft() t V = [0]*N [0]*N V s = i i s V = [0]*N [0]*N V a, b = map( int, input().split()) map( int, input().split()) a
from collections import deque
N = int( input())
E = [[] for _ in range(N)]
V = [0]*N
for _ in range(N-1):
    a, b = map( int, input().split())
    a, b = a-1, b-1
    E[a].append(b)
    E[b].append(a)
    V[a] += 1
    V[b] += 1
C = list(map( int, input().split()))
C.sort(reverse=True)
for i in range(N):
    if V[i] == 1:
        s = i
        break
q = deque([s])
V = [0]*N
l = 0
for l in range(N):
    t = q.popleft()
    V[t] = C[l]
    for v in E[t]:
        if V[v] == 0:
            q.append(v)
print( sum(C) - C[0])
print( " ".join( map( str, V)))
