from collections import Counter
import sys
sys.setrecursionlimit(10 ** 7)

N = int(input())
edges = [[] for _ in range(N)]

for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

A = list(map(int, input().split()))
A.sort()
S = sum(A) - A[-1]

print(S)

ans = [0] * N
st = [(0, -1)]
while st:
    now, pr = st.pop()
    ans[now] = A.pop()
    for to in edges[now]:
        if to == pr:
            continue
        st.append((to, now))
print(*ans) from collections import Counter import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 N = int(input()) N int(input()) int input() input [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] edges = [[] for _ in range(N)] edges [[] for _ in range(N)] for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr) _ range(N - 1) range N - 1 N 1 fr, to = map(int, input().split()) fr map(int, input().split()) map int input().split() ().split () input split to fr -= 1 fr 1 to -= 1 to 1 edges[fr].append(to) [fr].append [fr] edges fr append to edges[to].append(fr) [to].append [to] edges to append fr A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split A.sort() A.sort A sort S = sum(A) - A[-1] S sum(A) - A[-1] sum(A) sum A A[-1] A -1 print(S) print S ans = [0] * N ans [0] * N [0] 0 N st = [(0, -1)] st [(0, -1)] (0, -1) 0 -1 while st:
    now, pr = st.pop()
    ans[now] = A.pop()
    for to in edges[now]:
        if to == pr:
            continue
        st.append((to, now)) st now, pr = st.pop() now st.pop() st.pop st pop pr ans[now] = A.pop() ans[now] ans now A.pop() A.pop A pop for to in edges[now]:
        if to == pr:
            continue
        st.append((to, now)) to edges[now] edges now if to == pr:
            continue
         to == pr to pr continue st.append((to, now)) st.append st append (to, now) to now print(*ans) print *ans ans pr = st.pop() st.pop() pr A = list(map(int, input().split())) list(map(int, input().split())) A to -= 1 1 to fr, to = map(int, input().split()) map(int, input().split()) fr to = map(int, input().split()) map(int, input().split()) to ans = [0] * N [0] * N ans now, pr = st.pop() st.pop() now N = int(input()) int(input()) N st = [(0, -1)] [(0, -1)] st edges = [[] for _ in range(N)] [[] for _ in range(N)] edges fr -= 1 1 fr S = sum(A) - A[-1] sum(A) - A[-1] S
from collections import Counter
import sys
sys.setrecursionlimit(10 ** 7)

N = int(input())
edges = [[] for _ in range(N)]

for _ in range(N - 1):
    fr, to = map(int, input().split())
    fr -= 1
    to -= 1
    edges[fr].append(to)
    edges[to].append(fr)

A = list(map(int, input().split()))
A.sort()
S = sum(A) - A[-1]

print(S)

ans = [0] * N
st = [(0, -1)]
while st:
    now, pr = st.pop()
    ans[now] = A.pop()
    for to in edges[now]:
        if to == pr:
            continue
        st.append((to, now))
print(*ans)
