import heapq
import sys
sys.setrecursionlimit(20000)
N = int(input())
ab = [tuple(map(int, input().split())) for i in range(N-1)]
c = list(map(int, input().split()))

print(sum(c) - max(c))
c = list(map(lambda x: x*(-1), c))
heapq.heapify(c)
d = [[0, i] for i in range(N)]
g = [set() for i in range(N)]
for a, b in ab:
    d[a-1][0] += 1
    d[b-1][0] += 1
    g[a-1].add(b-1)
    g[b-1].add(a-1)
d.sort(reverse=True, key=lambda x: x[0])
for i in range(N):
    d[i][0] = -1


ans = ["-1"]*N
ans[d[0][1]] = str(-1 * heapq.heappop(c))


def dfs(start):
    for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x)


dfs(d[0][1])
d.sort(key=lambda x: x[1])


print(" ".join(ans)) import heapq heapq import sys sys sys.setrecursionlimit(20000) sys.setrecursionlimit sys setrecursionlimit 20000 N = int(input()) N int(input()) int input() input tuple(map(int, input().split())) for i in range(N-1) for i in range(N-1) i range(N-1) range N-1 N 1 for i in range(N-1) tuple(map(int, input().split())) tuple map(int, input().split()) map int input().split() ().split () input split ab = [tuple(map(int, input().split())) for i in range(N-1)] ab [tuple(map(int, input().split())) for i in range(N-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 print(sum(c) - max(c)) print sum(c) - max(c) sum(c) sum c max(c) max c c = list(map(lambda x: x*(-1), c)) c list(map(lambda x: x*(-1), c)) list map(lambda x: x*(-1), c) map lambda x: x*(-1) x*(-1) x -1 x c heapq.heapify(c) heapq.heapify heapq heapify c [0, i] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [0, i] 0 i d = [[0, i] for i in range(N)] d [[0, i] for i in range(N)] set() for i in range(N) for i in range(N) i range(N) range N for i in range(N) set() set g = [set() for i in range(N)] g [set() for i in range(N)] for a, b in ab:
    d[a-1][0] += 1
    d[b-1][0] += 1
    g[a-1].add(b-1)
    g[b-1].add(a-1) a b ab d[a-1][0] += 1 d[a-1][0] [a-1] d a-1 a 1 0 1 d[b-1][0] += 1 d[b-1][0] [b-1] d b-1 b 1 0 1 g[a-1].add(b-1) [a-1].add [a-1] g a-1 a 1 add b-1 b 1 g[b-1].add(a-1) [b-1].add [b-1] g b-1 b 1 add a-1 a 1 d.sort(reverse=True, key=lambda x: x[0]) d.sort d sort reverse=True reverse True key=lambda x: x[0] key lambda x: x[0] x[0] x 0 x for i in range(N):
    d[i][0] = -1 i range(N) range N d[i][0] = -1 d[i][0] [i] d i 0 -1 ans = ["-1"]*N ans ["-1"]*N ["-1"] "-1" N ans[d[0][1]] = str(-1 * heapq.heappop(c)) ans[d[0][1]] ans d[0][1] [0] d 0 1 str(-1 * heapq.heappop(c)) str -1 * heapq.heappop(c) -1 heapq.heappop(c) heapq.heappop heapq heappop c def dfs(start):
    for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x) dfs for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x) x g[start] g start if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x) ans[x] == "-1" ans[x] ans x "-1" ans[x] = str(-1 * heapq.heappop(c)) ans[x] ans x str(-1 * heapq.heappop(c)) str -1 * heapq.heappop(c) -1 heapq.heappop(c) heapq.heappop heapq heappop c dfs(x) dfs x start start dfs(d[0][1]) dfs d[0][1] [0] d 0 1 d.sort(key=lambda x: x[1]) d.sort d sort key=lambda x: x[1] key lambda x: x[1] x[1] x 1 x print(" ".join(ans)) print " ".join(ans) " ".join " " join ans N = int(input()) int(input()) N ab = [tuple(map(int, input().split())) for i in range(N-1)] [tuple(map(int, input().split())) for i in range(N-1)] ab c = list(map(lambda x: x*(-1), c)) list(map(lambda x: x*(-1), c)) c def dfs(start):
    for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x) def dfs(start):
    for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x) dfs ans = ["-1"]*N ["-1"]*N ans g = [set() for i in range(N)] [set() for i in range(N)] g d = [[0, i] for i in range(N)] [[0, i] for i in range(N)] d c = list(map(int, input().split())) list(map(int, input().split())) c
import heapq
import sys
sys.setrecursionlimit(20000)
N = int(input())
ab = [tuple(map(int, input().split())) for i in range(N-1)]
c = list(map(int, input().split()))

print(sum(c) - max(c))
c = list(map(lambda x: x*(-1), c))
heapq.heapify(c)
d = [[0, i] for i in range(N)]
g = [set() for i in range(N)]
for a, b in ab:
    d[a-1][0] += 1
    d[b-1][0] += 1
    g[a-1].add(b-1)
    g[b-1].add(a-1)
d.sort(reverse=True, key=lambda x: x[0])
for i in range(N):
    d[i][0] = -1


ans = ["-1"]*N
ans[d[0][1]] = str(-1 * heapq.heappop(c))


def dfs(start):
    for x in g[start]:
        if ans[x] == "-1":
            ans[x] = str(-1 * heapq.heappop(c))
            dfs(x)


dfs(d[0][1])
d.sort(key=lambda x: x[1])


print(" ".join(ans))
