def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ab = [0] * (N-1)
    num = [0] * N
    l = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
    C = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = 0
    q = [0]
    i = 0
    while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

    print(sum(num)-C[0])
    print(*num)

if __name__ == '__main__':
    main() def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ab = [0] * (N-1)
    num = [0] * N
    l = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
    C = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = 0
    q = [0]
    i = 0
    while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

    print(sum(num)-C[0])
    print(*num) main import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline N = int(input()) N int(input()) int input() input ab = [0] * (N-1) ab [0] * (N-1) [0] 0 N-1 N 1 num = [0] * N num [0] * N [0] 0 N [] for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) [] l = [[] for _ in range(N)] l [[] for _ in range(N)] for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
     n range(N-1) range N-1 N 1 int(x)-1 for x in input().strip().split() for x in input().strip().split() x input().strip().split() ().strip().split ().strip() ().strip () input strip split for x in input().strip().split() int(x)-1 int(x) int x 1 a, b = [int(x)-1 for x in input().strip().split()] a [int(x)-1 for x in input().strip().split()] b l[a].append(b) [a].append [a] l a append b l[b].append(a) [b].append [b] l b append a int(x) for x in input().strip().split() for x in input().strip().split() x input().strip().split() ().strip().split ().strip() ().strip () input strip split for x in input().strip().split() int(x) int x C = sorted([int(x) for x in input().strip().split()], reverse=True) C sorted([int(x) for x in input().strip().split()], reverse=True) sorted [int(x) for x in input().strip().split()] reverse=True reverse True ans = 0 ans 0 q = [0] q [0] 0 i = 0 i 0 while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

     q c = q.pop() c q.pop() q.pop q pop num[c] = C[i] num[c] num c C[i] C i i += 1 i 1 for n in l[c]:
            if num[n] == 0:
                q.append(n)

     n l[c] l c if num[n] == 0:
                q.append(n)

     num[n] == 0 num[n] num n 0 q.append(n) q.append q append n print(sum(num)-C[0]) print sum(num)-C[0] sum(num) sum num C[0] C 0 print(*num) print *num num if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ab = [0] * (N-1)
    num = [0] * N
    l = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
    C = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = 0
    q = [0]
    i = 0
    while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

    print(sum(num)-C[0])
    print(*num) def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ab = [0] * (N-1)
    num = [0] * N
    l = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
    C = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = 0
    q = [0]
    i = 0
    while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

    print(sum(num)-C[0])
    print(*num) main
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    ab = [0] * (N-1)
    num = [0] * N
    l = [[] for _ in range(N)]
    for n in range(N-1):
        a, b = [int(x)-1 for x in input().strip().split()]
        l[a].append(b)
        l[b].append(a)
    C = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = 0
    q = [0]
    i = 0
    while q:
        c = q.pop()
        num[c] = C[i]
        i += 1
        for n in l[c]:
            if num[n] == 0:
                q.append(n)

    print(sum(num)-C[0])
    print(*num)

if __name__ == '__main__':
    main()