import sys
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
e = []
tree = [[] for i in range(n)]
w = [-1] * n
for i in range(n-1):
    a, b = lmi()
    a -= 1; b -= 1
    e.append([a, b])
    tree[a].append(b)
    tree[b].append(a)

c = lmi()
c.sort()

def dfs(x):
    if len(c) == 0:
        return
    if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i)

dfs(0)

print(sum([min(w[i],w[j]) for i, j in e]))
print(' '.join(map(str, w))) import sys sys sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def lmi(): return list(map(int, input().split())) lmi n = int(input()) n int(input()) int input() input e = [] e [] [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] tree = [[] for i in range(n)] tree [[] for i in range(n)] w = [-1] * n w [-1] * n [-1] -1 n for i in range(n-1):
    a, b = lmi()
    a -= 1; b -= 1
    e.append([a, b])
    tree[a].append(b)
    tree[b].append(a) i range(n-1) range n-1 n 1 a, b = lmi() a lmi() lmi b a -= 1 a 1 b -= 1 b 1 e.append([a, b]) e.append e append [a, b] a b tree[a].append(b) [a].append [a] tree a append b tree[b].append(a) [b].append [b] tree b append a c = lmi() c lmi() lmi c.sort() c.sort c sort def dfs(x):
    if len(c) == 0:
        return
    if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i) dfs if len(c) == 0:
        return
     len(c) == 0 len(c) len c 0 return if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i) w[x] == -1 w[x] w x -1 w[x] = c.pop() w[x] w x c.pop() c.pop c pop for i in tree[x]:
            dfs(i) i tree[x] tree x dfs(i) dfs i x x dfs(0) dfs 0 min(w[i],w[j]) for i, j in e for i, j in e i j e for i, j in e min(w[i],w[j]) min w[i] w i w[j] w j print(sum([min(w[i],w[j]) for i, j in e])) print sum([min(w[i],w[j]) for i, j in e]) sum [min(w[i],w[j]) for i, j in e] print(' '.join(map(str, w))) print ' '.join(map(str, w)) ' '.join ' ' join map(str, w) map str w a -= 1 1 a n = int(input()) int(input()) n def dfs(x):
    if len(c) == 0:
        return
    if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i) def dfs(x):
    if len(c) == 0:
        return
    if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i) dfs def lmi(): return list(map(int, input().split())) def lmi(): return list(map(int, input().split())) lmi a, b = lmi() lmi() a b = lmi() lmi() b w = [-1] * n [-1] * n w b -= 1 1 b e = [] [] e tree = [[] for i in range(n)] [[] for i in range(n)] tree c = lmi() lmi() c
import sys
sys.setrecursionlimit(10**7)
def lmi(): return list(map(int, input().split()))

n = int(input())
e = []
tree = [[] for i in range(n)]
w = [-1] * n
for i in range(n-1):
    a, b = lmi()
    a -= 1; b -= 1
    e.append([a, b])
    tree[a].append(b)
    tree[b].append(a)

c = lmi()
c.sort()

def dfs(x):
    if len(c) == 0:
        return
    if w[x] == -1:
        w[x] = c.pop()
        for i in tree[x]:
            dfs(i)

dfs(0)

print(sum([min(w[i],w[j]) for i, j in e]))
print(' '.join(map(str, w)))
