#!/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)]
for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a)
C = list(map(int,input().split()))
C.sort(reverse = True)
ans = [0]*n
ans[0] = C[0]
que = collections.deque([0])
i = 1
M = 0
while que:
  node = que.popleft()
  for vi in G[node]:
    if ans[vi] == 0:
      ans[vi] = C[i]
      i += 1
      que.append(vi)
      M += min(ans[vi],ans[node])
print(M)
print(*ans) 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)] for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a) 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 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 ans = [0]*n ans [0]*n [0] 0 n ans[0] = C[0] ans[0] ans 0 C[0] C 0 que = collections.deque([0]) que collections.deque([0]) collections.deque collections deque [0] 0 i = 1 i 1 M = 0 M 0 while que:
  node = que.popleft()
  for vi in G[node]:
    if ans[vi] == 0:
      ans[vi] = C[i]
      i += 1
      que.append(vi)
      M += min(ans[vi],ans[node]) que node = que.popleft() node que.popleft() que.popleft que popleft for vi in G[node]:
    if ans[vi] == 0:
      ans[vi] = C[i]
      i += 1
      que.append(vi)
      M += min(ans[vi],ans[node]) vi G[node] G node if ans[vi] == 0:
      ans[vi] = C[i]
      i += 1
      que.append(vi)
      M += min(ans[vi],ans[node]) ans[vi] == 0 ans[vi] ans vi 0 ans[vi] = C[i] ans[vi] ans vi C[i] C i i += 1 i 1 que.append(vi) que.append que append vi M += min(ans[vi],ans[node]) M min(ans[vi],ans[node]) min ans[vi] ans vi ans[node] ans node print(M) print M print(*ans) print *ans ans node = que.popleft() que.popleft() node inf = float('inf') float('inf') inf ans = 0 0 ans pro = 1 1 pro que = collections.deque([0]) collections.deque([0]) que b-=1 1 b ans = [0]*n [0]*n ans i = 1 1 i a,b = map(int,input().split()) map(int,input().split()) a C = list(map(int,input().split())) list(map(int,input().split())) C b = map(int,input().split()) map(int,input().split()) b a-=1 1 a input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') input count = 0 0 count n = int(input()) int(input()) n mod = 10**9+7 10**9+7 mod mans = inf inf mans M = 0 0 M G = [[] for i in range(n)] [[] for i in range(n)] G M += min(ans[vi],ans[node]) min(ans[vi],ans[node]) M i += 1 1 i
#!/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)]
for i in range(n-1):
  a,b = map(int,input().split())
  a-=1;b-=1
  G[a].append(b)
  G[b].append(a)
C = list(map(int,input().split()))
C.sort(reverse = True)
ans = [0]*n
ans[0] = C[0]
que = collections.deque([0])
i = 1
M = 0
while que:
  node = que.popleft()
  for vi in G[node]:
    if ans[vi] == 0:
      ans[vi] = C[i]
      i += 1
      que.append(vi)
      M += min(ans[vi],ans[node])
print(M)
print(*ans)