import sys
import collections
input = sys.stdin.readline
N = int(input())
edge = [list(map(int,input().split())) for _ in range(N-1)]
c = sorted(list(map(int,input().split())),reverse=True)
graph = [[] for _ in range(N)]
ans = [-1 for _ in range(N)]
queue = collections.deque()

for e in edge:
    graph[e[0]-1].append(e[1]-1)
    graph[e[1]-1].append(e[0]-1)

for i in range(N):
    if len(graph[i])==1:
        queue.append(i)
        ans[i] = c[0]
        break
k=1
while queue:
    node = queue.popleft()
    for adj in graph[node]:
        if ans[adj]==-1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
            
print(sum(c)-max(c))
print(*ans) import sys sys import collections collections input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input list(map(int,input().split())) for _ in range(N-1) for _ in range(N-1) _ range(N-1) range N-1 N 1 for _ in range(N-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split edge = [list(map(int,input().split())) for _ in range(N-1)] edge [list(map(int,input().split())) for _ in range(N-1)] c = sorted(list(map(int,input().split())),reverse=True) c sorted(list(map(int,input().split())),reverse=True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse=True reverse True [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] graph = [[] for _ in range(N)] graph [[] for _ in range(N)] -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 ans = [-1 for _ in range(N)] ans [-1 for _ in range(N)] queue = collections.deque() queue collections.deque() collections.deque collections deque for e in edge:
    graph[e[0]-1].append(e[1]-1)
    graph[e[1]-1].append(e[0]-1) e edge graph[e[0]-1].append(e[1]-1) [e[0]-1].append [e[0]-1] graph e[0]-1 e[0] e 0 1 append e[1]-1 e[1] e 1 1 graph[e[1]-1].append(e[0]-1) [e[1]-1].append [e[1]-1] graph e[1]-1 e[1] e 1 1 append e[0]-1 e[0] e 0 1 for i in range(N):
    if len(graph[i])==1:
        queue.append(i)
        ans[i] = c[0]
        break i range(N) range N if len(graph[i])==1:
        queue.append(i)
        ans[i] = c[0]
        break len(graph[i])==1 len(graph[i]) len graph[i] graph i 1 queue.append(i) queue.append queue append i ans[i] = c[0] ans[i] ans i c[0] c 0 break k=1 k 1 while queue:
    node = queue.popleft()
    for adj in graph[node]:
        if ans[adj]==-1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
             queue node = queue.popleft() node queue.popleft() queue.popleft queue popleft for adj in graph[node]:
        if ans[adj]==-1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
             adj graph[node] graph node if ans[adj]==-1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
             ans[adj]==-1 ans[adj] ans adj -1 ans[adj] = c[k] ans[adj] ans adj c[k] c k k += 1 k 1 queue.append(adj) queue.append queue append adj print(sum(c)-max(c)) print sum(c)-max(c) sum(c) sum c max(c) max c print(*ans) print *ans ans edge = [list(map(int,input().split())) for _ in range(N-1)] [list(map(int,input().split())) for _ in range(N-1)] edge N = int(input()) int(input()) N ans = [-1 for _ in range(N)] [-1 for _ in range(N)] ans c = sorted(list(map(int,input().split())),reverse=True) sorted(list(map(int,input().split())),reverse=True) c input = sys.stdin.readline sys.stdin.readline input queue = collections.deque() collections.deque() queue node = queue.popleft() queue.popleft() node graph = [[] for _ in range(N)] [[] for _ in range(N)] graph k=1 1 k k += 1 1 k
import sys
import collections
input = sys.stdin.readline
N = int(input())
edge = [list(map(int,input().split())) for _ in range(N-1)]
c = sorted(list(map(int,input().split())),reverse=True)
graph = [[] for _ in range(N)]
ans = [-1 for _ in range(N)]
queue = collections.deque()

for e in edge:
    graph[e[0]-1].append(e[1]-1)
    graph[e[1]-1].append(e[0]-1)

for i in range(N):
    if len(graph[i])==1:
        queue.append(i)
        ans[i] = c[0]
        break
k=1
while queue:
    node = queue.popleft()
    for adj in graph[node]:
        if ans[adj]==-1:
            ans[adj] = c[k]
            k += 1
            queue.append(adj)
            
print(sum(c)-max(c))
print(*ans)