N = int(input())
cnt = dict()

nodes = [set() for _ in range(N+1)]

for _ in range(N-1):
    a, b = [int(x) for x in input().split()]
    cnt[a-1] = cnt.get(a-1,0) + 1
    cnt[b-1] = cnt.get(b-1,0) + 1
    nodes[a-1].add(b-1)
    nodes[b-1].add(a-1)


C = [int(x) for x in input().split()]
C = sorted(C, reverse=True)

Z = cnt.items()
Z = sorted(Z, key=lambda x:x[1], reverse=True)
# print(Z)

opennodes = {Z[0][0]}
closednodes = set()

ans = [-1] * N


for i in range(N):
    n = opennodes.pop()
    closednodes.add(n)
    ans[n] = C[i]

    for nn in nodes[n]:
        if not nn in closednodes:
            opennodes.add(nn)

print(sum(C) - max(C))
print(" ".join([str(x) for x in ans]))

 N = int(input()) N int(input()) int input() input cnt = dict() cnt dict() dict set() for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) set() set nodes = [set() for _ in range(N+1)] nodes [set() for _ in range(N+1)] for _ in range(N-1):
    a, b = [int(x) for x in input().split()]
    cnt[a-1] = cnt.get(a-1,0) + 1
    cnt[b-1] = cnt.get(b-1,0) + 1
    nodes[a-1].add(b-1)
    nodes[b-1].add(a-1) _ range(N-1) range N-1 N 1 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x a, b = [int(x) for x in input().split()] a [int(x) for x in input().split()] b cnt[a-1] = cnt.get(a-1,0) + 1 cnt[a-1] cnt a-1 a 1 cnt.get(a-1,0) + 1 cnt.get(a-1,0) cnt.get cnt get a-1 a 1 0 1 cnt[b-1] = cnt.get(b-1,0) + 1 cnt[b-1] cnt b-1 b 1 cnt.get(b-1,0) + 1 cnt.get(b-1,0) cnt.get cnt get b-1 b 1 0 1 nodes[a-1].add(b-1) [a-1].add [a-1] nodes a-1 a 1 add b-1 b 1 nodes[b-1].add(a-1) [b-1].add [b-1] nodes b-1 b 1 add a-1 a 1 int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x C = [int(x) for x in input().split()] C [int(x) for x in input().split()] C = sorted(C, reverse=True) C sorted(C, reverse=True) sorted C reverse=True reverse True Z = cnt.items() Z cnt.items() cnt.items cnt items Z = sorted(Z, key=lambda x:x[1], reverse=True) Z sorted(Z, key=lambda x:x[1], reverse=True) sorted Z key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x reverse=True reverse True opennodes = {Z[0][0]} opennodes {Z[0][0]} closednodes = set() closednodes set() set ans = [-1] * N ans [-1] * N [-1] -1 N for i in range(N):
    n = opennodes.pop()
    closednodes.add(n)
    ans[n] = C[i]

    for nn in nodes[n]:
        if not nn in closednodes:
            opennodes.add(nn) i range(N) range N n = opennodes.pop() n opennodes.pop() opennodes.pop opennodes pop closednodes.add(n) closednodes.add closednodes add n ans[n] = C[i] ans[n] ans n C[i] C i for nn in nodes[n]:
        if not nn in closednodes:
            opennodes.add(nn) nn nodes[n] nodes n if not nn in closednodes:
            opennodes.add(nn) not nn in closednodes nn in closednodes nn closednodes opennodes.add(nn) opennodes.add opennodes add nn print(sum(C) - max(C)) print sum(C) - max(C) sum(C) sum C max(C) max C str(x) for x in ans for x in ans x ans for x in ans str(x) str x print(" ".join([str(x) for x in ans])) print " ".join([str(x) for x in ans]) " ".join " " join [str(x) for x in ans] a, b = [int(x) for x in input().split()] [int(x) for x in input().split()] a opennodes = {Z[0][0]} {Z[0][0]} opennodes Z = sorted(Z, key=lambda x:x[1], reverse=True) sorted(Z, key=lambda x:x[1], reverse=True) Z closednodes = set() set() closednodes ans = [-1] * N [-1] * N ans Z = cnt.items() cnt.items() Z C = sorted(C, reverse=True) sorted(C, reverse=True) C C = [int(x) for x in input().split()] [int(x) for x in input().split()] C cnt = dict() dict() cnt b = [int(x) for x in input().split()] [int(x) for x in input().split()] b N = int(input()) int(input()) N nodes = [set() for _ in range(N+1)] [set() for _ in range(N+1)] nodes n = opennodes.pop() opennodes.pop() n
N = int(input())
cnt = dict()

nodes = [set() for _ in range(N+1)]

for _ in range(N-1):
    a, b = [int(x) for x in input().split()]
    cnt[a-1] = cnt.get(a-1,0) + 1
    cnt[b-1] = cnt.get(b-1,0) + 1
    nodes[a-1].add(b-1)
    nodes[b-1].add(a-1)


C = [int(x) for x in input().split()]
C = sorted(C, reverse=True)

Z = cnt.items()
Z = sorted(Z, key=lambda x:x[1], reverse=True)
# print(Z)

opennodes = {Z[0][0]}
closednodes = set()

ans = [-1] * N


for i in range(N):
    n = opennodes.pop()
    closednodes.add(n)
    ans[n] = C[i]

    for nn in nodes[n]:
        if not nn in closednodes:
            opennodes.add(nn)

print(sum(C) - max(C))
print(" ".join([str(x) for x in ans]))


