from collections import deque

n = int(input())
graph = {}
for i in range(1, n):
    a, b = map(int,input().split())
    try:
        if graph[a] is not None:
            graph[a].append(b)
    except:
        graph[a] = [b]
    try:
        if graph[b] is not None:
            graph[b].append(a)
    except:
        graph[b] = [a]
c = list(map(int, input().split()))
c.sort()
M = sum(c) - c[-1]
q = deque([1])
a = [0 for i in range(n)]
visited = []
while q:
    for x in list(q):
        visited.append(x)
        for y in graph[x]:
            if not y in visited:
                q.append(y)
        a[x-1] = str(c.pop())
        q.popleft()
print(M)
print(' '.join(a)) from collections import deque n = int(input()) n int(input()) int input() input graph = {} graph {} for i in range(1, n):
    a, b = map(int,input().split())
    try:
        if graph[a] is not None:
            graph[a].append(b)
    except:
        graph[a] = [b]
    try:
        if graph[b] is not None:
            graph[b].append(a)
    except:
        graph[b] = [a] i range(1, n) range 1 n a, b = map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b try:
        if graph[a] is not None:
            graph[a].append(b)
    except:
        graph[a] = [b]
     except:
        graph[a] = [b]
     graph[a] = [b] graph[a] graph a [b] b if graph[a] is not None:
            graph[a].append(b)
     graph[a] is not None graph[a] graph a None graph[a].append(b) [a].append [a] graph a append b try:
        if graph[b] is not None:
            graph[b].append(a)
    except:
        graph[b] = [a] except:
        graph[b] = [a] graph[b] = [a] graph[b] graph b [a] a if graph[b] is not None:
            graph[b].append(a)
     graph[b] is not None graph[b] graph b None graph[b].append(a) [b].append [b] graph 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() c.sort c sort M = sum(c) - c[-1] M sum(c) - c[-1] sum(c) sum c c[-1] c -1 q = deque([1]) q deque([1]) deque [1] 1 0 for i in range(n) for i in range(n) i range(n) range n for i in range(n) 0 a = [0 for i in range(n)] a [0 for i in range(n)] visited = [] visited [] while q:
    for x in list(q):
        visited.append(x)
        for y in graph[x]:
            if not y in visited:
                q.append(y)
        a[x-1] = str(c.pop())
        q.popleft() q for x in list(q):
        visited.append(x)
        for y in graph[x]:
            if not y in visited:
                q.append(y)
        a[x-1] = str(c.pop())
        q.popleft() x list(q) list q visited.append(x) visited.append visited append x for y in graph[x]:
            if not y in visited:
                q.append(y)
         y graph[x] graph x if not y in visited:
                q.append(y)
         not y in visited y in visited y visited q.append(y) q.append q append y a[x-1] = str(c.pop()) a[x-1] a x-1 x 1 str(c.pop()) str c.pop() c.pop c pop q.popleft() q.popleft q popleft print(M) print M print(' '.join(a)) print ' '.join(a) ' '.join ' ' join a M = sum(c) - c[-1] sum(c) - c[-1] M graph = {} {} graph b = map(int,input().split()) map(int,input().split()) b a, b = map(int,input().split()) map(int,input().split()) a a = [0 for i in range(n)] [0 for i in range(n)] a n = int(input()) int(input()) n c = list(map(int, input().split())) list(map(int, input().split())) c visited = [] [] visited q = deque([1]) deque([1]) q
from collections import deque

n = int(input())
graph = {}
for i in range(1, n):
    a, b = map(int,input().split())
    try:
        if graph[a] is not None:
            graph[a].append(b)
    except:
        graph[a] = [b]
    try:
        if graph[b] is not None:
            graph[b].append(a)
    except:
        graph[b] = [a]
c = list(map(int, input().split()))
c.sort()
M = sum(c) - c[-1]
q = deque([1])
a = [0 for i in range(n)]
visited = []
while q:
    for x in list(q):
        visited.append(x)
        for y in graph[x]:
            if not y in visited:
                q.append(y)
        a[x-1] = str(c.pop())
        q.popleft()
print(M)
print(' '.join(a))