#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
G = [[] for i in range(n)]
F = [-1]*n
AB = []
for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a)
  AB.append((a,b))
C = sorted(map(int,input().split()),reverse = True)
for i in range(n):
  G[i].sort(key = lambda x:len(G[x]))
m = max(range(n),key = lambda x: len(G[x]))
count = 1
que = collections.deque()
que.append(m)
F[m] = C[0]
while que:
  node = que.popleft()
  for vi in G[node]:
    if F[vi] != -1: continue
    F[vi] = C[count]
    count += 1
    que.append(vi)

for a,b in AB:
  ans += min(F[a],F[b])
print(ans)
print(*F)
# print(C)
 import sys, math, itertools, collections, bisect sys math itertools collections bisect input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') input lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') sys.stdin.buffer.readline().rstrip().decode('utf-8') sys.stdin.buffer.readline().rstrip().decode sys.stdin.buffer.readline().rstrip() sys.stdin.buffer.readline().rstrip sys.stdin.buffer.readline() sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline rstrip decode 'utf-8' inf = float('inf') inf float('inf') float 'inf' mod = 10**9+7 mod 10**9+7 10**9 10 9 7 mans = inf mans inf ans = 0 ans 0 count = 0 count 0 pro = 1 pro 1 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) [] G = [[] for i in range(n)] G [[] for i in range(n)] F = [-1]*n F [-1]*n [-1] -1 n AB = [] AB [] for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a)
  AB.append((a,b)) 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 a-=1 a 1 b-=1 b 1 G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a AB.append((a,b)) AB.append AB append (a,b) a b C = sorted(map(int,input().split()),reverse = True) C sorted(map(int,input().split()),reverse = True) sorted map(int,input().split()) map int input().split() ().split () input split reverse = True reverse True for i in range(n):
  G[i].sort(key = lambda x:len(G[x])) i range(n) range n G[i].sort(key = lambda x:len(G[x])) [i].sort [i] G i sort key = lambda x:len(G[x]) key lambda x:len(G[x]) len(G[x]) len G[x] G x x m = max(range(n),key = lambda x: len(G[x])) m max(range(n),key = lambda x: len(G[x])) max range(n) range n key = lambda x: len(G[x]) key lambda x: len(G[x]) len(G[x]) len G[x] G x x count = 1 count 1 que = collections.deque() que collections.deque() collections.deque collections deque que.append(m) que.append que append m F[m] = C[0] F[m] F m C[0] C 0 while que:
  node = que.popleft()
  for vi in G[node]:
    if F[vi] != -1: continue
    F[vi] = C[count]
    count += 1
    que.append(vi) que node = que.popleft() node que.popleft() que.popleft que popleft for vi in G[node]:
    if F[vi] != -1: continue
    F[vi] = C[count]
    count += 1
    que.append(vi) vi G[node] G node if F[vi] != -1: continue
     F[vi] != -1 F[vi] F vi -1 F[vi] = C[count] F[vi] F vi C[count] C count count += 1 count 1 que.append(vi) que.append que append vi for a,b in AB:
  ans += min(F[a],F[b]) a b AB ans += min(F[a],F[b]) ans min(F[a],F[b]) min F[a] F a F[b] F b print(ans) print ans print(*F) print *F F node = que.popleft() que.popleft() node input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') input count = 1 1 count ans += min(F[a],F[b]) min(F[a],F[b]) ans m = max(range(n),key = lambda x: len(G[x])) max(range(n),key = lambda x: len(G[x])) m count = 0 0 count mans = inf inf mans F = [-1]*n [-1]*n F pro = 1 1 pro mod = 10**9+7 10**9+7 mod b = map(int,input().split()) map(int,input().split()) b C = sorted(map(int,input().split()),reverse = True) sorted(map(int,input().split()),reverse = True) C inf = float('inf') float('inf') inf G = [[] for i in range(n)] [[] for i in range(n)] G que = collections.deque() collections.deque() que a-=1 1 a ans = 0 0 ans b-=1 1 b AB = [] [] AB count += 1 1 count n = int(input()) int(input()) n a,b = map(int,input().split()) map(int,input().split()) a
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
G = [[] for i in range(n)]
F = [-1]*n
AB = []
for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a)
  AB.append((a,b))
C = sorted(map(int,input().split()),reverse = True)
for i in range(n):
  G[i].sort(key = lambda x:len(G[x]))
m = max(range(n),key = lambda x: len(G[x]))
count = 1
que = collections.deque()
que.append(m)
F[m] = C[0]
while que:
  node = que.popleft()
  for vi in G[node]:
    if F[vi] != -1: continue
    F[vi] = C[count]
    count += 1
    que.append(vi)

for a,b in AB:
  ans += min(F[a],F[b])
print(ans)
print(*F)
# print(C)

