import sys
from collections import deque
input = sys.stdin.readline

n = int(input())
E = [[] for i in range(n)]

for i in range(n-1):
  a, b = map(int, input().split())
  E[a-1].append(b-1)
  E[b-1].append(a-1)

c = list(map(int, input().split()))
c.sort()
nums = deque(c)
ans = [0] * n
  
def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur])

dfs(0, -1)

print(sum(c[:-1]))
print(' '.join(ans)) import sys sys from collections import deque input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline 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) [] E = [[] for i in range(n)] E [[] for i in range(n)] for i in range(n-1):
  a, b = map(int, input().split())
  E[a-1].append(b-1)
  E[b-1].append(a-1) 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 E[a-1].append(b-1) [a-1].append [a-1] E a-1 a 1 append b-1 b 1 E[b-1].append(a-1) [b-1].append [b-1] E b-1 b 1 append a-1 a 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() c.sort c sort nums = deque(c) nums deque(c) deque c ans = [0] * n ans [0] * n [0] 0 n def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur]) dfs stack = deque([[cur, pre]]) stack deque([[cur, pre]]) deque [[cur, pre]] [cur, pre] cur pre while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur]) stack cur, pre = stack.pop() cur stack.pop() stack.pop stack pop pre num = nums.pop() num nums.pop() nums.pop nums pop ans[cur] = str(num) ans[cur] ans cur str(num) str num for e in E[cur]:
            if e != pre:
                stack.append([e, cur]) e E[cur] E cur if e != pre:
                stack.append([e, cur]) e != pre e pre stack.append([e, cur]) stack.append stack append [e, cur] e cur cur cur pre pre dfs(0, -1) dfs 0 -1 print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 print(' '.join(ans)) print ' '.join(ans) ' '.join ' ' join ans input = sys.stdin.readline sys.stdin.readline input n = int(input()) int(input()) n def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur]) def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur]) dfs b = map(int, input().split()) map(int, input().split()) b ans = [0] * n [0] * n ans a, b = map(int, input().split()) map(int, input().split()) a c = list(map(int, input().split())) list(map(int, input().split())) c E = [[] for i in range(n)] [[] for i in range(n)] E nums = deque(c) deque(c) nums
import sys
from collections import deque
input = sys.stdin.readline

n = int(input())
E = [[] for i in range(n)]

for i in range(n-1):
  a, b = map(int, input().split())
  E[a-1].append(b-1)
  E[b-1].append(a-1)

c = list(map(int, input().split()))
c.sort()
nums = deque(c)
ans = [0] * n
  
def dfs(cur, pre):
    stack = deque([[cur, pre]])
    while stack:
        cur, pre = stack.pop()
        num = nums.pop()
        ans[cur] = str(num)
        for e in E[cur]:
            if e != pre:
                stack.append([e, cur])

dfs(0, -1)

print(sum(c[:-1]))
print(' '.join(ans))