import sys
sys.setrecursionlimit(10000000)
n = int(input())
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
c = list(map(int,input().split()))
c.sort()
node = [0]*n
used = [False]*n

def dfs(cur,point):
    used[cur] = True
    for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
    return point
node[0] = c.pop()
# print(dfs(0,-1,0))
print(dfs(0,0))
print(*node) import sys sys sys.setrecursionlimit(10000000) sys.setrecursionlimit sys setrecursionlimit 10000000 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) [] G = [[] for _ in range(n)] G [[] for _ in range(n)] for i in range(n-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1) i 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-1].append(b-1) [a-1].append [a-1] G a-1 a 1 append b-1 b 1 G[b-1].append(a-1) [b-1].append [b-1] G 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 node = [0]*n node [0]*n [0] 0 n used = [False]*n used [False]*n [False] False n def dfs(cur,point):
    used[cur] = True
    for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
    return point dfs used[cur] = True used[cur] used cur True for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
     nx G[cur] G cur if used[nx]: continue
         used[nx] used nx t = c.pop() t c.pop() c.pop c pop node[nx] = t node[nx] node nx t point += min(node[cur],node[nx]) point min(node[cur],node[nx]) min node[cur] node cur node[nx] node nx point = dfs(nx,point) point dfs(nx,point) dfs nx point return point point cur cur point point node[0] = c.pop() node[0] node 0 c.pop() c.pop c pop print(dfs(0,0)) print dfs(0,0) dfs 0 0 print(*node) print *node node G = [[] for _ in range(n)] [[] for _ in range(n)] G b = map(int,input().split()) map(int,input().split()) b c = list(map(int,input().split())) list(map(int,input().split())) c def dfs(cur,point):
    used[cur] = True
    for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
    return point def dfs(cur,point):
    used[cur] = True
    for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
    return point dfs n = int(input()) int(input()) n node = [0]*n [0]*n node used = [False]*n [False]*n used a,b = map(int,input().split()) map(int,input().split()) a
import sys
sys.setrecursionlimit(10000000)
n = int(input())
G = [[] for _ in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
c = list(map(int,input().split()))
c.sort()
node = [0]*n
used = [False]*n

def dfs(cur,point):
    used[cur] = True
    for nx in G[cur]:
        if used[nx]: continue
        t = c.pop()
        node[nx] = t
        point += min(node[cur],node[nx])
        point = dfs(nx,point)
    return point
node[0] = c.pop()
# print(dfs(0,-1,0))
print(dfs(0,0))
print(*node)