#!/usr/bin/env python3

import sys, math, itertools, collections, bisect

input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')

inf = float('inf') ;mod = 10**9+7

mans = inf ;ans = 0 ;count = 0 ;pro = 1



n = int(eval(input()))

G = [[] for i in range(n)]

F = [-1]*n

AB = []

for i in range(n-1):

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

  a-=1;b-=1

  G[a].append(b)

  G[b].append(a)

  AB.append((a,b))

C = sorted(map(int,input().split()),reverse = True)

for i in range(n):

  G[i].sort(key = lambda x:len(G[x]))

m = max(list(range(n)),key = lambda x: len(G[x]))

count = 1

que = collections.deque()

que.append(m)

F[m] = C[0]

while que:

  node = que.popleft()

  for vi in G[node]:

    if F[vi] != -1: continue

    F[vi] = C[count]

    count += 1

    que.append(vi)



for a,b in AB:

  ans += min(F[a],F[b])

print(ans)

print((*F))

# print(C)



#!/usr/bin/env python3

import sys, math, itertools, collections, bisect

input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')

inf = float('inf') ;mod = 10**9+7

mans = inf ;ans = 0 ;count = 0 ;pro = 1



n = int(eval(input()))

G = [[] for i in range(n)]

for i in range(n-1):

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

  a-=1;b-=1

  G[a].append(b)

  G[b].append(a)

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

C.sort(reverse = True)

ans = [0]*n

ans[0] = C[0]

que = collections.deque([0])

i = 1

M = 0

while que:

  node = que.popleft()

  for vi in G[node]:

    if ans[vi] == 0:

      ans[vi] = C[i]

      i += 1

      que.append(vi)

      M += min(ans[vi],ans[node])

print(M)

print((*ans))
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect

input = lambda: sys.stdin.buffer.readline().rstrip().decode("utf-8")
inf = float("inf")
mod = 10**9 + 7
mans = inf
ans = 0
count = 0
pro = 1
n = int(eval(input()))
G = [[] for i in range(n)]
F = [-1] * n
AB = []
for i in range(n - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    AB.append((a, b))
C = sorted(map(int, input().split()), reverse=True)
for i in range(n):
    G[i].sort(key=lambda x: len(G[x]))
m = max(list(range(n)), key=lambda x: len(G[x]))
count = 1
que = collections.deque()
que.append(m)
F[m] = C[0]
while que:
    node = que.popleft()
    for vi in G[node]:
        if F[vi] != -1:
            continue
        F[vi] = C[count]
        count += 1
        que.append(vi)
for a, b in AB:
    ans += min(F[a], F[b])
print(ans)
print((*F))
# print(C)

#!/usr/bin/env python3
import sys, math, itertools, collections, bisect

input = lambda: sys.stdin.buffer.readline().rstrip().decode("utf-8")
inf = float("inf")
mod = 10**9 + 7
mans = inf
ans = 0
count = 0
pro = 1
n = int(eval(input()))
G = [[] for i in range(n)]
for i in range(n - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
C = list(map(int, input().split()))
C.sort(reverse=True)
ans = [0] * n
ans[0] = C[0]
que = collections.deque([0])
i = 1
M = 0
while que:
    node = que.popleft()
    for vi in G[node]:
        if ans[vi] == 0:
            ans[vi] = C[i]
            i += 1
            que.append(vi)
            M += min(ans[vi], ans[node])
print(M)
print((*ans))

-F = [-1] * n -AB = [] -    AB.append((a, b)) -C = sorted(map(int, input().split()), reverse=True) -for i in range(n): -    G[i].sort(key=lambda x: len(G[x])) -m = max(list(range(n)), key=lambda x: len(G[x])) -count = 1 -que = collections.deque() -que.append(m) -F[m] = C[0] +C = list(map(int, input().split())) +C.sort(reverse=True) +ans = [0] * n +ans[0] = C[0] +que = collections.deque([0]) +i = 1 +M = 0 -        if F[vi] != -1: -            continue -        F[vi] = C[count] -        count += 1 -        que.append(vi) -for a, b in AB: -    ans += min(F[a], F[b]) -print(ans) -print((*F)) -# print(C) +        if ans[vi] == 0: +            ans[vi] = C[i] +            i += 1 +            que.append(vi) +            M += min(ans[vi], ans[node]) +print(M) +print((*ans))