from collections import deque

def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)
    C = list(map(int, input().split()))
    C.sort(reverse=True)
    ans = [0] * N
    Q = deque([0])
    cnt = 0
    ans[0] = C[0]
    while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

    print(sum(C) - max(C))
    print(*ans)


if __name__ == "__main__":
    main() from collections import deque def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)
    C = list(map(int, input().split()))
    C.sort(reverse=True)
    ans = [0] * N
    Q = deque([0])
    cnt = 0
    ans[0] = C[0]
    while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

    print(sum(C) - max(C))
    print(*ans) 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 i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[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 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a 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 ans = [0] * N ans [0] * N [0] 0 N Q = deque([0]) Q deque([0]) deque [0] 0 cnt = 0 cnt 0 ans[0] = C[0] ans[0] ans 0 C[0] C 0 while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

     Q s = Q.pop() s Q.pop() Q.pop Q pop for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

     v edge[s] edge s if ans[v]:
                continue
             ans[v] ans v continue cnt += 1 cnt 1 ans[v] = C[cnt] ans[v] ans v C[cnt] C cnt Q.append(v) Q.append Q append v print(sum(C) - max(C)) print sum(C) - max(C) sum(C) sum C max(C) max C print(*ans) print *ans ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)
    C = list(map(int, input().split()))
    C.sort(reverse=True)
    ans = [0] * N
    Q = deque([0])
    cnt = 0
    ans[0] = C[0]
    while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

    print(sum(C) - max(C))
    print(*ans) def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)
    C = list(map(int, input().split()))
    C.sort(reverse=True)
    ans = [0] * N
    Q = deque([0])
    cnt = 0
    ans[0] = C[0]
    while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

    print(sum(C) - max(C))
    print(*ans) main
from collections import deque

def main():
    N = int(input())
    edge = [[] for _ in range(N)]
    for i in range(N-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        edge[a].append(b)
        edge[b].append(a)
    C = list(map(int, input().split()))
    C.sort(reverse=True)
    ans = [0] * N
    Q = deque([0])
    cnt = 0
    ans[0] = C[0]
    while Q:
        s = Q.pop()
        for v in edge[s]:
            if ans[v]:
                continue
            cnt += 1
            ans[v] = C[cnt]
            Q.append(v)

    print(sum(C) - max(C))
    print(*ans)


if __name__ == "__main__":
    main()