from sys import stdin
from collections import defaultdict,deque
N = int(stdin.readline().rstrip())
adj = defaultdict(set)

for i in range(N-1):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    adj[a].add(b)
    adj[b].add(a)
    
C = [int(x) for x in stdin.readline().rstrip().split()]

ans = sum(C) - max(C)
print(ans)

visit_p = deque()
visit_p.append(1)
visited = [False]*(N+1)
visited[1] = True

C.sort(reverse=True)
 
print(C[0],end=" ")
cnt = 1
ans_lst = [0]*(N+1)
while visit_p:
    now_v = visit_p.popleft()
    for v in adj[now_v]:
        if visited[v]:
            continue
        ans_lst[v] = C[cnt]
        cnt += 1
        visited[v] = True
        visit_p.append(v)
for i in ans_lst[2:]:
    print(i,end=" ") from sys import stdin from collections import defaultdict,deque N = int(stdin.readline().rstrip()) N int(stdin.readline().rstrip()) int stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip adj = defaultdict(set) adj defaultdict(set) defaultdict set for i in range(N-1):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    adj[a].add(b)
    adj[b].add(a)
     i range(N-1) range N-1 N 1 int(x) for x in stdin.readline().rstrip().split() for x in stdin.readline().rstrip().split() x stdin.readline().rstrip().split() stdin.readline().rstrip().split stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip split for x in stdin.readline().rstrip().split() int(x) int x a,b = [int(x) for x in stdin.readline().rstrip().split()] a [int(x) for x in stdin.readline().rstrip().split()] b adj[a].add(b) [a].add [a] adj a add b adj[b].add(a) [b].add [b] adj b add a int(x) for x in stdin.readline().rstrip().split() for x in stdin.readline().rstrip().split() x stdin.readline().rstrip().split() stdin.readline().rstrip().split stdin.readline().rstrip() stdin.readline().rstrip stdin.readline() stdin.readline stdin readline rstrip split for x in stdin.readline().rstrip().split() int(x) int x C = [int(x) for x in stdin.readline().rstrip().split()] C [int(x) for x in stdin.readline().rstrip().split()] ans = sum(C) - max(C) ans sum(C) - max(C) sum(C) sum C max(C) max C print(ans) print ans visit_p = deque() visit_p deque() deque visit_p.append(1) visit_p.append visit_p append 1 visited = [False]*(N+1) visited [False]*(N+1) [False] False N+1 N 1 visited[1] = True visited[1] visited 1 True C.sort(reverse=True) C.sort C sort reverse=True reverse True print(C[0],end=" ") print C[0] C 0 end=" " end " " cnt = 1 cnt 1 ans_lst = [0]*(N+1) ans_lst [0]*(N+1) [0] 0 N+1 N 1 while visit_p:
    now_v = visit_p.popleft()
    for v in adj[now_v]:
        if visited[v]:
            continue
        ans_lst[v] = C[cnt]
        cnt += 1
        visited[v] = True
        visit_p.append(v) visit_p now_v = visit_p.popleft() now_v visit_p.popleft() visit_p.popleft visit_p popleft for v in adj[now_v]:
        if visited[v]:
            continue
        ans_lst[v] = C[cnt]
        cnt += 1
        visited[v] = True
        visit_p.append(v) v adj[now_v] adj now_v if visited[v]:
            continue
         visited[v] visited v continue ans_lst[v] = C[cnt] ans_lst[v] ans_lst v C[cnt] C cnt cnt += 1 cnt 1 visited[v] = True visited[v] visited v True visit_p.append(v) visit_p.append visit_p append v for i in ans_lst[2:]:
    print(i,end=" ") i ans_lst[2:] ans_lst 2: 2 print(i,end=" ") print i end=" " end " " adj = defaultdict(set) defaultdict(set) adj ans = sum(C) - max(C) sum(C) - max(C) ans b = [int(x) for x in stdin.readline().rstrip().split()] [int(x) for x in stdin.readline().rstrip().split()] b visit_p = deque() deque() visit_p visited = [False]*(N+1) [False]*(N+1) visited cnt += 1 1 cnt cnt = 1 1 cnt N = int(stdin.readline().rstrip()) int(stdin.readline().rstrip()) N C = [int(x) for x in stdin.readline().rstrip().split()] [int(x) for x in stdin.readline().rstrip().split()] C ans_lst = [0]*(N+1) [0]*(N+1) ans_lst now_v = visit_p.popleft() visit_p.popleft() now_v a,b = [int(x) for x in stdin.readline().rstrip().split()] [int(x) for x in stdin.readline().rstrip().split()] a
from sys import stdin
from collections import defaultdict,deque
N = int(stdin.readline().rstrip())
adj = defaultdict(set)

for i in range(N-1):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    adj[a].add(b)
    adj[b].add(a)
    
C = [int(x) for x in stdin.readline().rstrip().split()]

ans = sum(C) - max(C)
print(ans)

visit_p = deque()
visit_p.append(1)
visited = [False]*(N+1)
visited[1] = True

C.sort(reverse=True)
 
print(C[0],end=" ")
cnt = 1
ans_lst = [0]*(N+1)
while visit_p:
    now_v = visit_p.popleft()
    for v in adj[now_v]:
        if visited[v]:
            continue
        ans_lst[v] = C[cnt]
        cnt += 1
        visited[v] = True
        visit_p.append(v)
for i in ans_lst[2:]:
    print(i,end=" ")