from collections import deque
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
C = sorted(list(map(int,input().split())))
A = [0 for _ in range(N+1)]
hist = [0 for _ in range(N+1)]
que = deque([1])
hist[1] = 1
cur = 0
while que:
    i = que[-1]
    if len(G[i])==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
    flag = 2
    for j in G[i]:
        if hist[j]==0:
            que.append(j)
            hist[j] = 1
            flag = 1
    if flag==2 and hist[i]==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
cnt = 0
hist = {(i,j):0 for i in range(1,N+1) for j in G[i]}
for i in range(1,N+1):
    for j in G[i]:
        if hist[(i,j)]==0:
            cnt += min(A[i],A[j])
            hist[(i,j)] = 1
            hist[(j,i)] = 1
print(cnt)
print(*A[1:]) from collections import deque N = int(input()) N int(input()) int input() input i:[] for i in range(1,N+1) for i in range(1,N+1) i range(1,N+1) range 1 N+1 N 1 for i in range(1,N+1) i [] G = {i:[] for i in range(1,N+1)} G {i:[] for i in range(1,N+1)} for _ in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a) _ range(N-1) range N-1 N 1 a,b = map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b G[a].append(b) [a].append [a] G a append b G[b].append(a) [b].append [b] G b append a C = sorted(list(map(int,input().split()))) C sorted(list(map(int,input().split()))) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 A = [0 for _ in range(N+1)] A [0 for _ in range(N+1)] 0 for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) 0 hist = [0 for _ in range(N+1)] hist [0 for _ in range(N+1)] que = deque([1]) que deque([1]) deque [1] 1 hist[1] = 1 hist[1] hist 1 1 cur = 0 cur 0 while que:
    i = que[-1]
    if len(G[i])==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
    flag = 2
    for j in G[i]:
        if hist[j]==0:
            que.append(j)
            hist[j] = 1
            flag = 1
    if flag==2 and hist[i]==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop() que i = que[-1] i que[-1] que -1 if len(G[i])==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
     len(G[i])==1 len(G[i]) len G[i] G i 1 A[i] = C[cur] A[i] A i C[cur] C cur cur += 1 cur 1 hist[i] = 2 hist[i] hist i 2 que.pop() que.pop que pop flag = 2 flag 2 for j in G[i]:
        if hist[j]==0:
            que.append(j)
            hist[j] = 1
            flag = 1
     j G[i] G i if hist[j]==0:
            que.append(j)
            hist[j] = 1
            flag = 1
     hist[j]==0 hist[j] hist j 0 que.append(j) que.append que append j hist[j] = 1 hist[j] hist j 1 flag = 1 flag 1 if flag==2 and hist[i]==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop() flag==2 and hist[i]==1 flag==2 flag 2 hist[i]==1 hist[i] hist i 1 A[i] = C[cur] A[i] A i C[cur] C cur cur += 1 cur 1 hist[i] = 2 hist[i] hist i 2 que.pop() que.pop que pop cnt = 0 cnt 0 (i,j):0 for i in range(1,N+1) for j in G[i] for i in range(1,N+1) for j in G[i] i range(1,N+1) range 1 N+1 N 1 for j in G[i] j G[i] G i for j in G[i] (i,j) i j 0 hist = {(i,j):0 for i in range(1,N+1) for j in G[i]} hist {(i,j):0 for i in range(1,N+1) for j in G[i]} for i in range(1,N+1):
    for j in G[i]:
        if hist[(i,j)]==0:
            cnt += min(A[i],A[j])
            hist[(i,j)] = 1
            hist[(j,i)] = 1 i range(1,N+1) range 1 N+1 N 1 for j in G[i]:
        if hist[(i,j)]==0:
            cnt += min(A[i],A[j])
            hist[(i,j)] = 1
            hist[(j,i)] = 1 j G[i] G i if hist[(i,j)]==0:
            cnt += min(A[i],A[j])
            hist[(i,j)] = 1
            hist[(j,i)] = 1 hist[(i,j)]==0 hist[(i,j)] hist (i,j) i j 0 cnt += min(A[i],A[j]) cnt min(A[i],A[j]) min A[i] A i A[j] A j hist[(i,j)] = 1 hist[(i,j)] hist (i,j) i j 1 hist[(j,i)] = 1 hist[(j,i)] hist (j,i) j i 1 print(cnt) print cnt print(*A[1:]) print *A[1:] A[1:] A 1: 1 cur += 1 1 cur que = deque([1]) deque([1]) que a,b = map(int,input().split()) map(int,input().split()) a flag = 2 2 flag flag = 1 1 flag C = sorted(list(map(int,input().split()))) sorted(list(map(int,input().split()))) C N = int(input()) int(input()) N hist = {(i,j):0 for i in range(1,N+1) for j in G[i]} {(i,j):0 for i in range(1,N+1) for j in G[i]} hist hist = [0 for _ in range(N+1)] [0 for _ in range(N+1)] hist cur = 0 0 cur A = [0 for _ in range(N+1)] [0 for _ in range(N+1)] A b = map(int,input().split()) map(int,input().split()) b cnt = 0 0 cnt i = que[-1] que[-1] i G = {i:[] for i in range(1,N+1)} {i:[] for i in range(1,N+1)} G cur += 1 1 cur cnt += min(A[i],A[j]) min(A[i],A[j]) cnt
from collections import deque
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
C = sorted(list(map(int,input().split())))
A = [0 for _ in range(N+1)]
hist = [0 for _ in range(N+1)]
que = deque([1])
hist[1] = 1
cur = 0
while que:
    i = que[-1]
    if len(G[i])==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
    flag = 2
    for j in G[i]:
        if hist[j]==0:
            que.append(j)
            hist[j] = 1
            flag = 1
    if flag==2 and hist[i]==1:
        A[i] = C[cur]
        cur += 1
        hist[i] = 2
        que.pop()
cnt = 0
hist = {(i,j):0 for i in range(1,N+1) for j in G[i]}
for i in range(1,N+1):
    for j in G[i]:
        if hist[(i,j)]==0:
            cnt += min(A[i],A[j])
            hist[(i,j)] = 1
            hist[(j,i)] = 1
print(cnt)
print(*A[1:])