import sys
input = sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

    a = list(map(int,input().split()))
    a.sort(reverse = True)
    put = [-1 for _ in range(N)]
    put[0] = a[0]
    q = deque([0])
    i = 1
    while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
    print(sum(a)-a[0])
    print(*put)
    
if __name__ == "__main__":
    main()
 import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline from collections import deque def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

    a = list(map(int,input().split()))
    a.sort(reverse = True)
    put = [-1 for _ in range(N)]
    put[0] = a[0]
    q = deque([0])
    i = 1
    while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
    print(sum(a)-a[0])
    print(*put)
     main N = int(input()) N int(input()) int input() input [] 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 _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

     _ range(N-1) range N-1 N 1 l,r = map(int,input().split()) l map(int,input().split()) map int input().split() ().split () input split r edge[l-1].append(r-1) [l-1].append [l-1] edge l-1 l 1 append r-1 r 1 edge[r-1].append(l-1) [r-1].append [r-1] edge r-1 r 1 append l-1 l 1 a = list(map(int,input().split())) a list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split a.sort(reverse = True) a.sort a sort reverse = True reverse True -1 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) -1 put = [-1 for _ in range(N)] put [-1 for _ in range(N)] put[0] = a[0] put[0] put 0 a[0] a 0 q = deque([0]) q deque([0]) deque [0] 0 i = 1 i 1 while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
     q now = q.popleft() now q.popleft() q.popleft q popleft for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
     fol edge[now] edge now if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
     put[fol] != -1 put[fol] put fol -1 continue put[fol] = a[i] put[fol] put fol a[i] a i i += 1 i 1 q.append(fol) q.append q append fol print(sum(a)-a[0]) print sum(a)-a[0] sum(a) sum a a[0] a 0 print(*put) print *put put if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

    a = list(map(int,input().split()))
    a.sort(reverse = True)
    put = [-1 for _ in range(N)]
    put[0] = a[0]
    q = deque([0])
    i = 1
    while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
    print(sum(a)-a[0])
    print(*put)
     def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

    a = list(map(int,input().split()))
    a.sort(reverse = True)
    put = [-1 for _ in range(N)]
    put[0] = a[0]
    q = deque([0])
    i = 1
    while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
    print(sum(a)-a[0])
    print(*put)
     main input = sys.stdin.readline sys.stdin.readline input
import sys
input = sys.stdin.readline
from collections import deque

def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for _ in range(N-1):
        l,r = map(int,input().split())
        edge[l-1].append(r-1)
        edge[r-1].append(l-1)

    a = list(map(int,input().split()))
    a.sort(reverse = True)
    put = [-1 for _ in range(N)]
    put[0] = a[0]
    q = deque([0])
    i = 1
    while q:
        now = q.popleft()
        for fol in edge[now]:
            if put[fol] != -1:
                continue
            else:
                put[fol] = a[i]
                i += 1
                q.append(fol)
    
    print(sum(a)-a[0])
    print(*put)
    
if __name__ == "__main__":
    main()

