import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    way = [[] for _ in [0]*n]
    for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
    c = [int(i) for i in ipt().split()]
    c.sort(reverse=True)
    ans = [0]*n
    q = queue.Queue()
    ans[0] = c[0]
    ni = 1
    q.put(0)
    sum = 0
    while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

    print(sum)
    print(" ".join(map(str,ans)))



    return

if __name__ == '__main__':
    main() import math math import queue queue from collections import deque,defaultdict import heapq heapq from sys import stdin,setrecursionlimit ipt = stdin.readline ipt stdin.readline stdin readline setrecursionlimit(10**7) setrecursionlimit 10**7 10 7 def main():
    n = int(ipt())
    way = [[] for _ in [0]*n]
    for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
    c = [int(i) for i in ipt().split()]
    c.sort(reverse=True)
    ans = [0]*n
    q = queue.Queue()
    ans[0] = c[0]
    ni = 1
    q.put(0)
    sum = 0
    while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

    print(sum)
    print(" ".join(map(str,ans)))



    return main n = int(ipt()) n int(ipt()) int ipt() ipt [] for _ in [0]*n for _ in [0]*n _ [0]*n [0] 0 n for _ in [0]*n [] way = [[] for _ in [0]*n] way [[] for _ in [0]*n] for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
     i [0]*(n-1) [0] 0 n-1 n 1 a,b = map(int,ipt().split()) a map(int,ipt().split()) map int ipt().split() ().split () ipt split b way[a-1].append(b-1) [a-1].append [a-1] way a-1 a 1 append b-1 b 1 way[b-1].append(a-1) [b-1].append [b-1] way b-1 b 1 append a-1 a 1 int(i) for i in ipt().split() for i in ipt().split() i ipt().split() ().split () ipt split for i in ipt().split() int(i) int i c = [int(i) for i in ipt().split()] c [int(i) for i in ipt().split()] c.sort(reverse=True) c.sort c sort reverse=True reverse True ans = [0]*n ans [0]*n [0] 0 n q = queue.Queue() q queue.Queue() queue.Queue queue Queue ans[0] = c[0] ans[0] ans 0 c[0] c 0 ni = 1 ni 1 q.put(0) q.put q put 0 sum = 0 sum 0 while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

     ni < n ni n qi = q.get() qi q.get() q.get q get for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

     i way[qi] way qi if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

     ans[i] == 0 ans[i] ans i 0 ans[i] = c[ni] ans[i] ans i c[ni] c ni sum += c[ni] sum c[ni] c ni ni += 1 ni 1 q.put(i) q.put q put i print(sum) print sum print(" ".join(map(str,ans))) print " ".join(map(str,ans)) " ".join " " join map(str,ans) map str ans return if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main ipt = stdin.readline stdin.readline ipt def main():
    n = int(ipt())
    way = [[] for _ in [0]*n]
    for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
    c = [int(i) for i in ipt().split()]
    c.sort(reverse=True)
    ans = [0]*n
    q = queue.Queue()
    ans[0] = c[0]
    ni = 1
    q.put(0)
    sum = 0
    while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

    print(sum)
    print(" ".join(map(str,ans)))



    return def main():
    n = int(ipt())
    way = [[] for _ in [0]*n]
    for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
    c = [int(i) for i in ipt().split()]
    c.sort(reverse=True)
    ans = [0]*n
    q = queue.Queue()
    ans[0] = c[0]
    ni = 1
    q.put(0)
    sum = 0
    while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

    print(sum)
    print(" ".join(map(str,ans)))



    return main
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    way = [[] for _ in [0]*n]
    for i in [0]*(n-1):
        a,b = map(int,ipt().split())
        way[a-1].append(b-1)
        way[b-1].append(a-1)
    c = [int(i) for i in ipt().split()]
    c.sort(reverse=True)
    ans = [0]*n
    q = queue.Queue()
    ans[0] = c[0]
    ni = 1
    q.put(0)
    sum = 0
    while ni < n:
        qi = q.get()
        for i in way[qi]:
            if ans[i] == 0:
                ans[i] = c[ni]
                sum += c[ni]
                ni += 1
                q.put(i)

    print(sum)
    print(" ".join(map(str,ans)))



    return

if __name__ == '__main__':
    main()
