from collections import deque

n = int(input())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
l = sorted(list(map(int,input().split())),reverse=True)
ans = [-1]*n
for i in range(n):
    if len(e[i]) == 1:
        q = deque([])
        q.append(i)
        ans[i] = l[0]
        now = 1
        while q:
            b = q.popleft()
            for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
        count = 0
        for j in range(n):
            for k in e[j]:
                count += min(ans[j],ans[k])
        print(count//2)
        print(*ans)
        exit()


 from collections import deque n = int(input()) n int(input()) int input() input [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] e = [[] for i in range(n)] e [[] for i in range(n)] for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a) i range(n-1) range n-1 n 1 a,b = map(int,input().split()) a map(int,input().split()) map int input().split() ().split () input split b a -= 1 a 1 b -= 1 b 1 e[a].append(b) [a].append [a] e a append b e[b].append(a) [b].append [b] e b append a l = sorted(list(map(int,input().split())),reverse=True) l sorted(list(map(int,input().split())),reverse=True) sorted list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split reverse=True reverse True ans = [-1]*n ans [-1]*n [-1] -1 n for i in range(n):
    if len(e[i]) == 1:
        q = deque([])
        q.append(i)
        ans[i] = l[0]
        now = 1
        while q:
            b = q.popleft()
            for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
        count = 0
        for j in range(n):
            for k in e[j]:
                count += min(ans[j],ans[k])
        print(count//2)
        print(*ans)
        exit() i range(n) range n if len(e[i]) == 1:
        q = deque([])
        q.append(i)
        ans[i] = l[0]
        now = 1
        while q:
            b = q.popleft()
            for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
        count = 0
        for j in range(n):
            for k in e[j]:
                count += min(ans[j],ans[k])
        print(count//2)
        print(*ans)
        exit() len(e[i]) == 1 len(e[i]) len e[i] e i 1 q = deque([]) q deque([]) deque [] q.append(i) q.append q append i ans[i] = l[0] ans[i] ans i l[0] l 0 now = 1 now 1 while q:
            b = q.popleft()
            for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
         q b = q.popleft() b q.popleft() q.popleft q popleft for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
         nex e[b] e b if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
         ans[nex] == -1 ans[nex] ans nex -1 ans[nex] = l[now] ans[nex] ans nex l[now] l now now += 1 now 1 q.append(nex) q.append q append nex count = 0 count 0 for j in range(n):
            for k in e[j]:
                count += min(ans[j],ans[k])
         j range(n) range n for k in e[j]:
                count += min(ans[j],ans[k])
         k e[j] e j count += min(ans[j],ans[k]) count min(ans[j],ans[k]) min ans[j] ans j ans[k] ans k print(count//2) print count//2 count 2 print(*ans) print *ans ans exit() exit a,b = map(int,input().split()) map(int,input().split()) a count = 0 0 count ans = [-1]*n [-1]*n ans now = 1 1 now b -= 1 1 b q = deque([]) deque([]) q b = map(int,input().split()) map(int,input().split()) b a -= 1 1 a l = sorted(list(map(int,input().split())),reverse=True) sorted(list(map(int,input().split())),reverse=True) l b = q.popleft() q.popleft() b n = int(input()) int(input()) n now += 1 1 now count += min(ans[j],ans[k]) min(ans[j],ans[k]) count e = [[] for i in range(n)] [[] for i in range(n)] e
from collections import deque

n = int(input())
e = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append(b)
    e[b].append(a)
l = sorted(list(map(int,input().split())),reverse=True)
ans = [-1]*n
for i in range(n):
    if len(e[i]) == 1:
        q = deque([])
        q.append(i)
        ans[i] = l[0]
        now = 1
        while q:
            b = q.popleft()
            for nex in e[b]:
                if ans[nex] == -1:
                    ans[nex] = l[now]
                    now += 1
                    q.append(nex)
        count = 0
        for j in range(n):
            for k in e[j]:
                count += min(ans[j],ans[k])
        print(count//2)
        print(*ans)
        exit()



