n = int(eval(input()))

g = [[] for _ in range(n)]

for i in range(n-1):

    a, b = list(map(int, input().split()))

    a, b = a-1, b-1

    g[a].append(b)

    g[b].append(a)



C = list(map(int, input().split()))

C.sort(reverse=True)



order = []

par = [-1]*n

s = []

visit = [-1]*n

s.append(0)

visit[0] = 0

while s:

    v = s.pop()

    order.append(v)

    for u in g[v]:

        if u == par[v]:

            continue

        par[u] = v

        s.append(u)



order.reverse()

ans = [0]*n

m = 0

for v in order:

    ans[v] = C.pop()

    if par[v] != -1:

        m += ans[v]

print(m)

print((*ans))
import sys

input = sys.stdin.buffer.readline



n = int(eval(input()))

g = [[] for _ in range(n)]

for i in range(n-1):

    a, b = list(map(int, input().split()))

    a, b = a-1, b-1

    g[a].append(b)

    g[b].append(a)

C = list(map(int, input().split()))

C.sort()



s = []

parent = [-1]*n

order = []

s.append(0)

while s:

    v = s.pop()

    order.append(v)

    for u in g[v]:

        if parent[v] == u:

            continue

        parent[u] = v

        s.append(u)



order.reverse()

ans = 0

D = [0]*n

for i in range(n):

    v = order[i]

    D[v] = C[i]

    if i != n-1:

        ans += C[i]

print(ans)

print((*D))

n = int(eval(input()))
g = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = list(map(int, input().split()))
    a, b = a - 1, b - 1
    g[a].append(b)
    g[b].append(a)
C = list(map(int, input().split()))
C.sort(reverse=True)
order = []
par = [-1] * n
s = []
visit = [-1] * n
s.append(0)
visit[0] = 0
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if u == par[v]:
            continue
        par[u] = v
        s.append(u)
order.reverse()
ans = [0] * n
m = 0
for v in order:
    ans[v] = C.pop()
    if par[v] != -1:
        m += ans[v]
print(m)
print((*ans))

import sys

input = sys.stdin.buffer.readline
n = int(eval(input()))
g = [[] for _ in range(n)]
for i in range(n - 1):
    a, b = list(map(int, input().split()))
    a, b = a - 1, b - 1
    g[a].append(b)
    g[b].append(a)
C = list(map(int, input().split()))
C.sort()
s = []
parent = [-1] * n
order = []
s.append(0)
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] == u:
            continue
        parent[u] = v
        s.append(u)
order.reverse()
ans = 0
D = [0] * n
for i in range(n):
    v = order[i]
    D[v] = C[i]
    if i != n - 1:
        ans += C[i]
print(ans)
print((*D))

+import sys + +input = sys.stdin.buffer.readline -C.sort(reverse=True) +C.sort() +s = [] +parent = [-1] * n -par = [-1] * n -s = [] -visit = [-1] * n -visit[0] = 0 -        if u == par[v]: +        if parent[v] == u: -        par[u] = v +        parent[u] = v -ans = [0] * n -m = 0 -for v in order: -    ans[v] = C.pop() -    if par[v] != -1: -        m += ans[v] -print(m) -print((*ans)) +ans = 0 +D = [0] * n +for i in range(n): +    v = order[i] +    D[v] = C[i] +    if i != n - 1: +        ans += C[i] +print(ans) +print((*D))