# coding: utf-8
# Your code here!
from collections import deque

def bfs(num):
    if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
    return

N=int(input())

ans=[-1]*N
way=[[]for i in range(N)]
visited=[0]*N

for _ in range(N-1):
    a,b=map(int,input().split())
    way[a-1].append(b-1)
    way[b-1].append(a-1)

C=list(map(int,input().split()))
C.sort()
#print(C)


d=deque([0])

while d:
    temp=d.popleft()
    if visited[temp]==0:
        bfs(temp)

ans=0
for i in range(N):
    for item in way[i]:
        ans+=min(visited[i],visited[item])

print(ans//2)
print(*visited)
     from collections import deque def bfs(num):
    if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
    return bfs if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
     visited[num]==0 visited[num] visited num 0 visited[num]=C.pop(-1) visited[num] visited num C.pop(-1) C.pop C pop -1 for item in way[num]:
            d.append(item)
     item way[num] way num d.append(item) d.append d append item return num num N=int(input()) N int(input()) int input() input ans=[-1]*N ans [-1]*N [-1] -1 N []for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] way=[[]for i in range(N)] way [[]for i in range(N)] visited=[0]*N visited [0]*N [0] 0 N for _ in range(N-1):
    a,b=map(int,input().split())
    way[a-1].append(b-1)
    way[b-1].append(a-1) _ 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 way[a-1].append(b-1) [a-1].append [a-1] way a-1 a 1 append b-1 b 1 way[b-1].append(a-1) [b-1].append [b-1] way b-1 b 1 append a-1 a 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 C.sort() C.sort C sort d=deque([0]) d deque([0]) deque [0] 0 while d:
    temp=d.popleft()
    if visited[temp]==0:
        bfs(temp) d temp=d.popleft() temp d.popleft() d.popleft d popleft if visited[temp]==0:
        bfs(temp) visited[temp]==0 visited[temp] visited temp 0 bfs(temp) bfs temp ans=0 ans 0 for i in range(N):
    for item in way[i]:
        ans+=min(visited[i],visited[item]) i range(N) range N for item in way[i]:
        ans+=min(visited[i],visited[item]) item way[i] way i ans+=min(visited[i],visited[item]) ans min(visited[i],visited[item]) min visited[i] visited i visited[item] visited item print(ans//2) print ans//2 ans 2 print(*visited) print *visited visited visited=[0]*N [0]*N visited C=list(map(int,input().split())) list(map(int,input().split())) C d=deque([0]) deque([0]) d def bfs(num):
    if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
    return def bfs(num):
    if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
    return bfs way=[[]for i in range(N)] [[]for i in range(N)] way temp=d.popleft() d.popleft() temp ans+=min(visited[i],visited[item]) min(visited[i],visited[item]) ans N=int(input()) int(input()) N b=map(int,input().split()) map(int,input().split()) b ans=0 0 ans ans=[-1]*N [-1]*N ans a,b=map(int,input().split()) map(int,input().split()) a
# coding: utf-8
# Your code here!
from collections import deque

def bfs(num):
    if visited[num]==0:
        visited[num]=C.pop(-1)
        for item in way[num]:
            d.append(item)
    return

N=int(input())

ans=[-1]*N
way=[[]for i in range(N)]
visited=[0]*N

for _ in range(N-1):
    a,b=map(int,input().split())
    way[a-1].append(b-1)
    way[b-1].append(a-1)

C=list(map(int,input().split()))
C.sort()
#print(C)


d=deque([0])

while d:
    temp=d.popleft()
    if visited[temp]==0:
        bfs(temp)

ans=0
for i in range(N):
    for item in way[i]:
        ans+=min(visited[i],visited[item])

print(ans//2)
print(*visited)
    
