import sys
from collections import deque
n = int(input())
graph = {i : [] for i in range(n)}
for i in range(n-1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
c_ls = [int(i) for i in sys.stdin.readline().split()]
c_ls.sort(reverse=True)
costs = [0 for i in range(n)]
que = deque([0])
already = {0}
i = 0
while len(que) > 0:
    cur = que.popleft()
    costs[cur] = c_ls[i]
    i += 1
    for _next in graph[cur]:
        if _next not in already:
            que.append(_next)
            already.add(_next)
print(sum(c_ls[1:]))
print(" ".join([str(i) for i in costs])) import sys sys from collections import deque n = int(input()) n int(input()) int input() input i : [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) i [] graph = {i : [] for i in range(n)} graph {i : [] for i in range(n)} for i in range(n-1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a) i range(n-1) range n-1 n 1 int(i) for i in sys.stdin.readline().split() for i in sys.stdin.readline().split() i sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for i in sys.stdin.readline().split() int(i) int i a, b = [int(i) for i in sys.stdin.readline().split()] a [int(i) for i in sys.stdin.readline().split()] b a -= 1 a 1 b -= 1 b 1 graph[a].append(b) [a].append [a] graph a append b graph[b].append(a) [b].append [b] graph b append a int(i) for i in sys.stdin.readline().split() for i in sys.stdin.readline().split() i sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split for i in sys.stdin.readline().split() int(i) int i c_ls = [int(i) for i in sys.stdin.readline().split()] c_ls [int(i) for i in sys.stdin.readline().split()] c_ls.sort(reverse=True) c_ls.sort c_ls sort reverse=True reverse True 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 costs = [0 for i in range(n)] costs [0 for i in range(n)] que = deque([0]) que deque([0]) deque [0] 0 already = {0} already {0} i = 0 i 0 while len(que) > 0:
    cur = que.popleft()
    costs[cur] = c_ls[i]
    i += 1
    for _next in graph[cur]:
        if _next not in already:
            que.append(_next)
            already.add(_next) len(que) > 0 len(que) len que 0 cur = que.popleft() cur que.popleft() que.popleft que popleft costs[cur] = c_ls[i] costs[cur] costs cur c_ls[i] c_ls i i += 1 i 1 for _next in graph[cur]:
        if _next not in already:
            que.append(_next)
            already.add(_next) _next graph[cur] graph cur if _next not in already:
            que.append(_next)
            already.add(_next) _next not in already _next already que.append(_next) que.append que append _next already.add(_next) already.add already add _next print(sum(c_ls[1:])) print sum(c_ls[1:]) sum c_ls[1:] c_ls 1: 1 str(i) for i in costs for i in costs i costs for i in costs str(i) str i print(" ".join([str(i) for i in costs])) print " ".join([str(i) for i in costs]) " ".join " " join [str(i) for i in costs] already = {0} {0} already c_ls = [int(i) for i in sys.stdin.readline().split()] [int(i) for i in sys.stdin.readline().split()] c_ls i += 1 1 i n = int(input()) int(input()) n que = deque([0]) deque([0]) que cur = que.popleft() que.popleft() cur i = 0 0 i graph = {i : [] for i in range(n)} {i : [] for i in range(n)} graph b -= 1 1 b b = [int(i) for i in sys.stdin.readline().split()] [int(i) for i in sys.stdin.readline().split()] b costs = [0 for i in range(n)] [0 for i in range(n)] costs a -= 1 1 a a, b = [int(i) for i in sys.stdin.readline().split()] [int(i) for i in sys.stdin.readline().split()] a
import sys
from collections import deque
n = int(input())
graph = {i : [] for i in range(n)}
for i in range(n-1):
    a, b = [int(i) for i in sys.stdin.readline().split()]
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
c_ls = [int(i) for i in sys.stdin.readline().split()]
c_ls.sort(reverse=True)
costs = [0 for i in range(n)]
que = deque([0])
already = {0}
i = 0
while len(que) > 0:
    cur = que.popleft()
    costs[cur] = c_ls[i]
    i += 1
    for _next in graph[cur]:
        if _next not in already:
            que.append(_next)
            already.add(_next)
print(sum(c_ls[1:]))
print(" ".join([str(i) for i in costs]))