import numpy as np 
N = int(input())
# N = 1000
# G = np.zeros((N,N),dtype = np.bool)

linked = [[] for i in range(N)]
pairs = []

for i in range(N-1):
    a,b = map(int,input().split())
    # a,b = 1,2
    pairs.append((a,b))
    linked[a-1].append(b-1)
    linked[b-1].append(a-1)

Cs = list(sorted(map(int,input().split()),reverse=True))
# Cs = [1 for i in range(N)]

# print(Cs)

links = np.array([0 for i in range(N)])

for i in range(N):
    links[i] = len(linked[i])

# minag = np.argmin(links)
nodeval = np.array([0 for i in range(N)])

stack = [1]

visited = [False for i in range(N)]

while(len(stack)>0):
    minag = stack.pop()
    nodeval[minag] = Cs.pop(0)
    for l in linked[minag]:
        if not visited[l]:
            stack.append(l)
    visited[minag] = True
    # print(nodeval)

sumval=0
for p in pairs:
    sumval += min(nodeval[p[0]-1], nodeval[p[1]-1] )

print(sumval)
for i,l in enumerate(nodeval):
    print(l,end=" " if i!=N-1 else "\n") import numpy as np numpy 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) [] linked = [[] for i in range(N)] linked [[] for i in range(N)] pairs = [] pairs [] for i in range(N-1):
    a,b = map(int,input().split())
    # a,b = 1,2
    pairs.append((a,b))
    linked[a-1].append(b-1)
    linked[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 pairs.append((a,b)) pairs.append pairs append (a,b) a b linked[a-1].append(b-1) [a-1].append [a-1] linked a-1 a 1 append b-1 b 1 linked[b-1].append(a-1) [b-1].append [b-1] linked b-1 b 1 append a-1 a 1 Cs = list(sorted(map(int,input().split()),reverse=True)) Cs list(sorted(map(int,input().split()),reverse=True)) list sorted(map(int,input().split()),reverse=True) sorted map(int,input().split()) map int input().split() ().split () input split 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 links = np.array([0 for i in range(N)]) links np.array([0 for i in range(N)]) np.array np array [0 for i in range(N)] for i in range(N):
    links[i] = len(linked[i])

# minag = np.argmin(links) i range(N) range N links[i] = len(linked[i]) links[i] links i len(linked[i]) len linked[i] linked i 0 for i in range(N) for i in range(N) i range(N) range N for i in range(N) 0 nodeval = np.array([0 for i in range(N)]) nodeval np.array([0 for i in range(N)]) np.array np array [0 for i in range(N)] stack = [1] stack [1] 1 False for i in range(N) for i in range(N) i range(N) range N for i in range(N) False visited = [False for i in range(N)] visited [False for i in range(N)] while(len(stack)>0):
    minag = stack.pop()
    nodeval[minag] = Cs.pop(0)
    for l in linked[minag]:
        if not visited[l]:
            stack.append(l)
    visited[minag] = True
    # print(nodeval) len(stack)>0 len(stack) len stack 0 minag = stack.pop() minag stack.pop() stack.pop stack pop nodeval[minag] = Cs.pop(0) nodeval[minag] nodeval minag Cs.pop(0) Cs.pop Cs pop 0 for l in linked[minag]:
        if not visited[l]:
            stack.append(l)
     l linked[minag] linked minag if not visited[l]:
            stack.append(l)
     not visited[l] visited[l] visited l stack.append(l) stack.append stack append l visited[minag] = True visited[minag] visited minag True sumval=0 sumval 0 for p in pairs:
    sumval += min(nodeval[p[0]-1], nodeval[p[1]-1] ) p pairs sumval += min(nodeval[p[0]-1], nodeval[p[1]-1] ) sumval min(nodeval[p[0]-1], nodeval[p[1]-1] ) min nodeval[p[0]-1] nodeval p[0]-1 p[0] p 0 1 nodeval[p[1]-1] nodeval p[1]-1 p[1] p 1 1 print(sumval) print sumval for i,l in enumerate(nodeval):
    print(l,end=" " if i!=N-1 else "\n") i l enumerate(nodeval) enumerate nodeval print(l,end=" " if i!=N-1 else "\n") print l end=" " if i!=N-1 else "\n" end " " if i!=N-1 else "\n" i!=N-1 i N-1 N 1 " " "\n" visited = [False for i in range(N)] [False for i in range(N)] visited b = map(int,input().split()) map(int,input().split()) b Cs = list(sorted(map(int,input().split()),reverse=True)) list(sorted(map(int,input().split()),reverse=True)) Cs sumval += min(nodeval[p[0]-1], nodeval[p[1]-1] ) min(nodeval[p[0]-1], nodeval[p[1]-1] ) sumval links = np.array([0 for i in range(N)]) np.array([0 for i in range(N)]) links a,b = map(int,input().split()) map(int,input().split()) a pairs = [] [] pairs stack = [1] [1] stack sumval=0 0 sumval linked = [[] for i in range(N)] [[] for i in range(N)] linked nodeval = np.array([0 for i in range(N)]) np.array([0 for i in range(N)]) nodeval N = int(input()) int(input()) N minag = stack.pop() stack.pop() minag
import numpy as np 
N = int(input())
# N = 1000
# G = np.zeros((N,N),dtype = np.bool)

linked = [[] for i in range(N)]
pairs = []

for i in range(N-1):
    a,b = map(int,input().split())
    # a,b = 1,2
    pairs.append((a,b))
    linked[a-1].append(b-1)
    linked[b-1].append(a-1)

Cs = list(sorted(map(int,input().split()),reverse=True))
# Cs = [1 for i in range(N)]

# print(Cs)

links = np.array([0 for i in range(N)])

for i in range(N):
    links[i] = len(linked[i])

# minag = np.argmin(links)
nodeval = np.array([0 for i in range(N)])

stack = [1]

visited = [False for i in range(N)]

while(len(stack)>0):
    minag = stack.pop()
    nodeval[minag] = Cs.pop(0)
    for l in linked[minag]:
        if not visited[l]:
            stack.append(l)
    visited[minag] = True
    # print(nodeval)

sumval=0
for p in pairs:
    sumval += min(nodeval[p[0]-1], nodeval[p[1]-1] )

print(sumval)
for i,l in enumerate(nodeval):
    print(l,end=" " if i!=N-1 else "\n")
