N = int(input())
AB = [[int(_) for _ in input().split()] for _ in range(N - 1)]
C = sorted([int(_) for _ in input().split()])
Pairs = [set() for _ in range(N)]
for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    Pairs[a].add(b)
    Pairs[b].add(a)
visited = [False] * N
visited[0] = True
count = [0] * N
stack = [0]
start = []
parent = [0] * N
while stack:
    fr = stack.pop()
    updated = False
    for to in Pairs[fr]:
        if not visited[to]:
            visited[to] = updated = True
            stack += [to]
            count[fr] += 1
            parent[to] = fr
    if not updated:
        start += [fr]
order = []
while start:
    fr = start.pop()
    if count[fr]:
        continue
    order += [fr]
    to = parent[fr]
    count[to] -= 1
    start += [to]
ans = [0] * N
for i in range(N):
    ans[order[i]] = C[i]
score = 0
for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    score += min([ans[a], ans[b]])
print(score)
print(*ans) N = int(input()) N int(input()) int input() input [int(_) for _ in 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) [int(_) for _ in input().split()] AB = [[int(_) for _ in input().split()] for _ in range(N - 1)] AB [[int(_) for _ in input().split()] for _ in range(N - 1)] int(_) for _ in input().split() for _ in input().split() _ input().split() ().split () input split for _ in input().split() int(_) int _ C = sorted([int(_) for _ in input().split()]) C sorted([int(_) for _ in input().split()]) sorted [int(_) for _ in input().split()] set() for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) set() set Pairs = [set() for _ in range(N)] Pairs [set() for _ in range(N)] for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    Pairs[a].add(b)
    Pairs[b].add(a) a b AB a -= 1 a 1 b -= 1 b 1 Pairs[a].add(b) [a].add [a] Pairs a add b Pairs[b].add(a) [b].add [b] Pairs b add a visited = [False] * N visited [False] * N [False] False N visited[0] = True visited[0] visited 0 True count = [0] * N count [0] * N [0] 0 N stack = [0] stack [0] 0 start = [] start [] parent = [0] * N parent [0] * N [0] 0 N while stack:
    fr = stack.pop()
    updated = False
    for to in Pairs[fr]:
        if not visited[to]:
            visited[to] = updated = True
            stack += [to]
            count[fr] += 1
            parent[to] = fr
    if not updated:
        start += [fr] stack fr = stack.pop() fr stack.pop() stack.pop stack pop updated = False updated False for to in Pairs[fr]:
        if not visited[to]:
            visited[to] = updated = True
            stack += [to]
            count[fr] += 1
            parent[to] = fr
     to Pairs[fr] Pairs fr if not visited[to]:
            visited[to] = updated = True
            stack += [to]
            count[fr] += 1
            parent[to] = fr
     not visited[to] visited[to] visited to visited[to] = updated = True visited[to] visited to True = updated = True updated True stack += [to] stack [to] to count[fr] += 1 count[fr] count fr 1 parent[to] = fr parent[to] parent to fr if not updated:
        start += [fr] not updated updated start += [fr] start [fr] fr order = [] order [] while start:
    fr = start.pop()
    if count[fr]:
        continue
    order += [fr]
    to = parent[fr]
    count[to] -= 1
    start += [to] start fr = start.pop() fr start.pop() start.pop start pop if count[fr]:
        continue
     count[fr] count fr continue order += [fr] order [fr] fr to = parent[fr] to parent[fr] parent fr count[to] -= 1 count[to] count to 1 start += [to] start [to] to ans = [0] * N ans [0] * N [0] 0 N for i in range(N):
    ans[order[i]] = C[i] i range(N) range N ans[order[i]] = C[i] ans[order[i]] ans order[i] order i C[i] C i score = 0 score 0 for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    score += min([ans[a], ans[b]]) a b AB a -= 1 a 1 b -= 1 b 1 score += min([ans[a], ans[b]]) score min([ans[a], ans[b]]) min [ans[a], ans[b]] ans[a] ans a ans[b] ans b print(score) print score print(*ans) print *ans ans start = [] [] start score = 0 0 score a -= 1 1 a parent = [0] * N [0] * N parent N = int(input()) int(input()) N score += min([ans[a], ans[b]]) min([ans[a], ans[b]]) score stack += [to] [to] stack stack = [0] [0] stack to = parent[fr] parent[fr] to visited = [False] * N [False] * N visited order += [fr] [fr] order fr = stack.pop() stack.pop() fr fr = start.pop() start.pop() fr updated = False False updated updated = True True updated order = [] [] order a -= 1 1 a C = sorted([int(_) for _ in input().split()]) sorted([int(_) for _ in input().split()]) C ans = [0] * N [0] * N ans start += [fr] [fr] start b -= 1 1 b Pairs = [set() for _ in range(N)] [set() for _ in range(N)] Pairs b -= 1 1 b AB = [[int(_) for _ in input().split()] for _ in range(N - 1)] [[int(_) for _ in input().split()] for _ in range(N - 1)] AB count = [0] * N [0] * N count start += [to] [to] start
N = int(input())
AB = [[int(_) for _ in input().split()] for _ in range(N - 1)]
C = sorted([int(_) for _ in input().split()])
Pairs = [set() for _ in range(N)]
for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    Pairs[a].add(b)
    Pairs[b].add(a)
visited = [False] * N
visited[0] = True
count = [0] * N
stack = [0]
start = []
parent = [0] * N
while stack:
    fr = stack.pop()
    updated = False
    for to in Pairs[fr]:
        if not visited[to]:
            visited[to] = updated = True
            stack += [to]
            count[fr] += 1
            parent[to] = fr
    if not updated:
        start += [fr]
order = []
while start:
    fr = start.pop()
    if count[fr]:
        continue
    order += [fr]
    to = parent[fr]
    count[to] -= 1
    start += [to]
ans = [0] * N
for i in range(N):
    ans[order[i]] = C[i]
score = 0
for a, b in AB:
    #0-indexed
    a -= 1
    b -= 1
    score += min([ans[a], ans[b]])
print(score)
print(*ans)
