from collections import deque
import sys
sys.setrecursionlimit(100000)

N=int(input())
AB=[list(map(int,input().split())) for _ in range(N-1)]
C=deque(sorted(map(int,input().split())))
edges=[[] for _ in range(N)]
for a,b in AB:
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

ps=[0]*N

def dfs(prev, curr):
    ps[curr]=C.pop()
    for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst)
dfs(-1,0)

ans=0
for a,b in AB:
    ans += min(ps[a-1],ps[b-1])

print(ans)
print(" ".join([str(p) for p in ps])) from collections import deque import sys sys sys.setrecursionlimit(100000) sys.setrecursionlimit sys setrecursionlimit 100000 N=int(input()) N int(input()) int input() input list(map(int,input().split())) for _ in range(N-1) for _ in range(N-1) _ range(N-1) range N-1 N 1 for _ in range(N-1) list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split AB=[list(map(int,input().split())) for _ in range(N-1)] AB [list(map(int,input().split())) for _ in range(N-1)] C=deque(sorted(map(int,input().split()))) C deque(sorted(map(int,input().split()))) deque sorted(map(int,input().split())) sorted map(int,input().split()) map int input().split() ().split () input split [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] edges=[[] for _ in range(N)] edges [[] for _ in range(N)] for a,b in AB:
    edges[a-1].append(b-1)
    edges[b-1].append(a-1) a b AB edges[a-1].append(b-1) [a-1].append [a-1] edges a-1 a 1 append b-1 b 1 edges[b-1].append(a-1) [b-1].append [b-1] edges b-1 b 1 append a-1 a 1 ps=[0]*N ps [0]*N [0] 0 N def dfs(prev, curr):
    ps[curr]=C.pop()
    for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst) dfs ps[curr]=C.pop() ps[curr] ps curr C.pop() C.pop C pop for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst) dst edges[curr] edges curr if prev==dst:
            continue
         prev==dst prev dst continue dfs(curr,dst) dfs curr dst prev prev curr curr dfs(-1,0) dfs -1 0 ans=0 ans 0 for a,b in AB:
    ans += min(ps[a-1],ps[b-1]) a b AB ans += min(ps[a-1],ps[b-1]) ans min(ps[a-1],ps[b-1]) min ps[a-1] ps a-1 a 1 ps[b-1] ps b-1 b 1 print(ans) print ans str(p) for p in ps for p in ps p ps for p in ps str(p) str p print(" ".join([str(p) for p in ps])) print " ".join([str(p) for p in ps]) " ".join " " join [str(p) for p in ps] ans += min(ps[a-1],ps[b-1]) min(ps[a-1],ps[b-1]) ans def dfs(prev, curr):
    ps[curr]=C.pop()
    for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst) def dfs(prev, curr):
    ps[curr]=C.pop()
    for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst) dfs AB=[list(map(int,input().split())) for _ in range(N-1)] [list(map(int,input().split())) for _ in range(N-1)] AB N=int(input()) int(input()) N edges=[[] for _ in range(N)] [[] for _ in range(N)] edges ans=0 0 ans ps=[0]*N [0]*N ps C=deque(sorted(map(int,input().split()))) deque(sorted(map(int,input().split()))) C
from collections import deque
import sys
sys.setrecursionlimit(100000)

N=int(input())
AB=[list(map(int,input().split())) for _ in range(N-1)]
C=deque(sorted(map(int,input().split())))
edges=[[] for _ in range(N)]
for a,b in AB:
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

ps=[0]*N

def dfs(prev, curr):
    ps[curr]=C.pop()
    for dst in edges[curr]:
        if prev==dst:
            continue
        dfs(curr,dst)
dfs(-1,0)

ans=0
for a,b in AB:
    ans += min(ps[a-1],ps[b-1])

print(ans)
print(" ".join([str(p) for p in ps]))
