def main():

    N = int(input())
    count = [[] for i in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

    max_l = 0
    max_i = -1
    for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

    c = list(map(int, input().split()))
    c.sort(reverse = True)
    # print(c)

    cur = [max_i]
    val = [0 for _ in range(N)]
    i = 0
    val[max_i] = c[i]
    i += 1
    ans = 0
    while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

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

if __name__ == '__main__':
    main() def main():

    N = int(input())
    count = [[] for i in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

    max_l = 0
    max_i = -1
    for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

    c = list(map(int, input().split()))
    c.sort(reverse = True)
    # print(c)

    cur = [max_i]
    val = [0 for _ in range(N)]
    i = 0
    val[max_i] = c[i]
    i += 1
    ans = 0
    while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

    print(ans)
    print(" ".join(map(str, val))) main 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) [] count = [[] for i in range(N)] count [[] for i in range(N)] for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

     _ 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 count[a-1].append(b-1) [a-1].append [a-1] count a-1 a 1 append b-1 b 1 count[b-1].append(a-1) [b-1].append [b-1] count b-1 b 1 append a-1 a 1 max_l = 0 max_l 0 max_i = -1 max_i -1 for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

     i range(N) range N if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

     len(count[i]) > max_l len(count[i]) len count[i] count i max_l max_l = len(count[i]) max_l len(count[i]) len count[i] count i max_i = i max_i i c = list(map(int, input().split())) c list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c.sort(reverse = True) c.sort c sort reverse = True reverse True cur = [max_i] cur [max_i] max_i 0 for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) 0 val = [0 for _ in range(N)] val [0 for _ in range(N)] i = 0 i 0 val[max_i] = c[i] val[max_i] val max_i c[i] c i i += 1 i 1 ans = 0 ans 0 while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

     cur temp = [] temp [] for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
         j cur for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
         k count[j] count j if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
         val[k] == 0 val[k] val k 0 val[k] = c[i] val[k] val k c[i] c i ans += c[i] ans c[i] c i i += 1 i 1 temp.append(k) temp.append temp append k cur = temp cur temp print(ans) print ans print(" ".join(map(str, val))) print " ".join(map(str, val)) " ".join " " join map(str, val) map str val if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():

    N = int(input())
    count = [[] for i in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

    max_l = 0
    max_i = -1
    for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

    c = list(map(int, input().split()))
    c.sort(reverse = True)
    # print(c)

    cur = [max_i]
    val = [0 for _ in range(N)]
    i = 0
    val[max_i] = c[i]
    i += 1
    ans = 0
    while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

    print(ans)
    print(" ".join(map(str, val))) def main():

    N = int(input())
    count = [[] for i in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

    max_l = 0
    max_i = -1
    for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

    c = list(map(int, input().split()))
    c.sort(reverse = True)
    # print(c)

    cur = [max_i]
    val = [0 for _ in range(N)]
    i = 0
    val[max_i] = c[i]
    i += 1
    ans = 0
    while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

    print(ans)
    print(" ".join(map(str, val))) main
def main():

    N = int(input())
    count = [[] for i in range(N)]
    for _ in range(N-1):
        a, b = map(int, input().split())
        count[a-1].append(b-1)
        count[b-1].append(a-1)

    max_l = 0
    max_i = -1
    for i in range(N):
        if len(count[i]) > max_l:
            max_l = len(count[i])
            max_i = i

    c = list(map(int, input().split()))
    c.sort(reverse = True)
    # print(c)

    cur = [max_i]
    val = [0 for _ in range(N)]
    i = 0
    val[max_i] = c[i]
    i += 1
    ans = 0
    while cur:
        temp = []
        for j in cur:
            for k in count[j]:
                if val[k] == 0:
                    val[k] = c[i]
                    ans += c[i]
                    i += 1
                    temp.append(k)
        cur = temp

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

if __name__ == '__main__':
    main()