import sys
sys.setrecursionlimit(10**8)
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
C = list(map(int,input().split()))
C.sort()
es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

score = 0
vscore = [-1]*N
vscore[0] = C.pop()

def dfs(v,p=-1):
    global score
    for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v)
dfs(0)

print(score)
print(*vscore) import sys sys sys.setrecursionlimit(10**8) sys.setrecursionlimit sys setrecursionlimit 10**8 10 8 N = int(input()) N int(input()) int input() input tuple(map(int,input().split())) for i in range(N-1) for i in range(N-1) i range(N-1) range N-1 N 1 for i in range(N-1) tuple(map(int,input().split())) tuple map(int,input().split()) map int input().split() ().split () input split AB = [tuple(map(int,input().split())) for i in range(N-1)] AB [tuple(map(int,input().split())) for i in range(N-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 [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] es = [[] for i in range(N)] es [[] for i in range(N)] for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a) a b AB a,b = a-1,b-1 a a-1 a 1 b b-1 b 1 es[a].append(b) [a].append [a] es a append b es[b].append(a) [b].append [b] es b append a score = 0 score 0 vscore = [-1]*N vscore [-1]*N [-1] -1 N vscore[0] = C.pop() vscore[0] vscore 0 C.pop() C.pop C pop def dfs(v,p=-1):
    global score
    for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v) dfs global score for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v) to es[v] es v if to==p: continue
         to==p to p vscore[to] = C.pop() vscore[to] vscore to C.pop() C.pop C pop score += min(vscore[v], vscore[to]) score min(vscore[v], vscore[to]) min vscore[v] vscore v vscore[to] vscore to dfs(to,v) dfs to v v v p=-1 p -1 dfs(0) dfs 0 print(score) print score print(*vscore) print *vscore vscore es = [[] for i in range(N)] [[] for i in range(N)] es def dfs(v,p=-1):
    global score
    for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v) def dfs(v,p=-1):
    global score
    for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v) dfs C = list(map(int,input().split())) list(map(int,input().split())) C vscore = [-1]*N [-1]*N vscore a,b = a-1,b-1 a-1 a N = int(input()) int(input()) N score = 0 0 score AB = [tuple(map(int,input().split())) for i in range(N-1)] [tuple(map(int,input().split())) for i in range(N-1)] AB b = a-1,b-1 b-1 b
import sys
sys.setrecursionlimit(10**8)
N = int(input())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
C = list(map(int,input().split()))
C.sort()
es = [[] for i in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

score = 0
vscore = [-1]*N
vscore[0] = C.pop()

def dfs(v,p=-1):
    global score
    for to in es[v]:
        if to==p: continue
        vscore[to] = C.pop()
        score += min(vscore[v], vscore[to])
        dfs(to,v)
dfs(0)

print(score)
print(*vscore)