import sys
sys.setrecursionlimit(100000)
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N-1)]
c = list(map(int, input().split()))

if N == 1:
    print(0)
    print(c[0])
    exit()

c.sort(reverse = True)
edge = [[] for _ in range(N)]
for a, b in ab:
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1]
search = [0]
res = 0
rese = [None for _ in range(N)]
for i in search:
    count = 0
    ans = 0
    anse = [None for _ in range(N)]
    anse[i] = c[0]
    dfs(i, -1)
    if res < ans:
        res = ans
        rese = anse
print(res)
print(" ".join(map(str, rese))) 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 = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split if N == 1:
    print(0)
    print(c[0])
    exit() N == 1 N 1 print(0) print 0 print(c[0]) print c[0] c 0 exit() exit c.sort(reverse = True) c.sort c sort reverse = True reverse True [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] edge = [[] for _ in range(N)] edge [[] for _ in range(N)] for a, b in ab:
    edge[a-1].append(b-1)
    edge[b-1].append(a-1) a b ab edge[a-1].append(b-1) [a-1].append [a-1] edge a-1 a 1 append b-1 b 1 edge[b-1].append(a-1) [b-1].append [b-1] edge b-1 b 1 append a-1 a 1 def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1] dfs global ans global count if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
     p != -1 p -1 anse[v] = c[count+1] anse[v] anse v c[count+1] c count+1 count 1 ans += min(anse[v], anse[p]) ans min(anse[v], anse[p]) min anse[v] anse v anse[p] anse p count += 1 count 1 for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1] nv edge[v] edge v if nv == p:
            continue
         nv == p nv p continue dfs(nv, v) dfs nv v v v p p search = [0] search [0] 0 res = 0 res 0 None for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) None rese = [None for _ in range(N)] rese [None for _ in range(N)] for i in search:
    count = 0
    ans = 0
    anse = [None for _ in range(N)]
    anse[i] = c[0]
    dfs(i, -1)
    if res < ans:
        res = ans
        rese = anse i search count = 0 count 0 ans = 0 ans 0 None for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) None anse = [None for _ in range(N)] anse [None for _ in range(N)] anse[i] = c[0] anse[i] anse i c[0] c 0 dfs(i, -1) dfs i -1 if res < ans:
        res = ans
        rese = anse res < ans res ans res = ans res ans rese = anse rese anse print(res) print res print(" ".join(map(str, rese))) print " ".join(map(str, rese)) " ".join " " join map(str, rese) map str rese search = [0] [0] search N = int(input()) int(input()) N c = list(map(int, input().split())) list(map(int, input().split())) c ab = [list(map(int, input().split())) for _ in range(N-1)] [list(map(int, input().split())) for _ in range(N-1)] ab edge = [[] for _ in range(N)] [[] for _ in range(N)] edge rese = anse anse rese count = 0 0 count anse = [None for _ in range(N)] [None for _ in range(N)] anse res = ans ans res def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1] def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1] dfs rese = [None for _ in range(N)] [None for _ in range(N)] rese res = 0 0 res ans = 0 0 ans
import sys
sys.setrecursionlimit(100000)
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N-1)]
c = list(map(int, input().split()))

if N == 1:
    print(0)
    print(c[0])
    exit()

c.sort(reverse = True)
edge = [[] for _ in range(N)]
for a, b in ab:
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)

def dfs(v, p):
    global ans
    global count
    if p != -1:
        anse[v] = c[count+1]
        ans += min(anse[v], anse[p])
        count += 1
    for nv in edge[v]:
        if nv == p:
            continue
        dfs(nv, v)

#search = [i for i, e in enumerate(edge) if len(e) == 1]
search = [0]
res = 0
rese = [None for _ in range(N)]
for i in search:
    count = 0
    ans = 0
    anse = [None for _ in range(N)]
    anse[i] = c[0]
    dfs(i, -1)
    if res < ans:
        res = ans
        rese = anse
print(res)
print(" ".join(map(str, rese)))