N = int(input())

Edge = [[]for _ in range(N)]
Num = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    Edge[a].append(b)
    Edge[b].append(a)
for _ in range(N):
    Num[len(Edge[_])].append(_)
C = list(map(int, input().split()))
C.sort()
cnt = 0
Ans = [-1] * N
ans = 0
for i in range(1, N):
    if cnt == N:
        break
    while Num[i]:
        if Num[0]:
            Ans[Num[0][0]] = C[cnt]
            cnt += 1
        x = Num[i].pop()
        if Ans[x] != -1:
            continue
        Ans[x] = C[cnt]
        ans += C[cnt] * i
        cnt += 1
        for y in Edge[x]:
            num = len(Edge[y])
            Num[num-1].append(y)
            Edge[x].remove(y)
            Edge[y].remove(x)

print(ans)
for i, a in enumerate(Ans):
    if i == N-1:
        print(a)
    else:
        print(a, end=' ') 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) for _ in range(N) _ range(N) range N for _ in range(N) [] Num = [[] for _ in range(N)] Num [[] for _ in range(N)] for _ in range(N-1):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    Edge[a].append(b)
    Edge[b].append(a) _ 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 for _ in range(N):
    Num[len(Edge[_])].append(_) _ range(N) range N Num[len(Edge[_])].append(_) [len(Edge[_])].append [len(Edge[_])] Num len(Edge[_]) len Edge[_] Edge _ append _ 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() C.sort C sort cnt = 0 cnt 0 Ans = [-1] * N Ans [-1] * N [-1] -1 N ans = 0 ans 0 for i in range(1, N):
    if cnt == N:
        break
    while Num[i]:
        if Num[0]:
            Ans[Num[0][0]] = C[cnt]
            cnt += 1
        x = Num[i].pop()
        if Ans[x] != -1:
            continue
        Ans[x] = C[cnt]
        ans += C[cnt] * i
        cnt += 1
        for y in Edge[x]:
            num = len(Edge[y])
            Num[num-1].append(y)
            Edge[x].remove(y)
            Edge[y].remove(x) i range(1, N) range 1 N if cnt == N:
        break
     cnt == N cnt N break while Num[i]:
        if Num[0]:
            Ans[Num[0][0]] = C[cnt]
            cnt += 1
        x = Num[i].pop()
        if Ans[x] != -1:
            continue
        Ans[x] = C[cnt]
        ans += C[cnt] * i
        cnt += 1
        for y in Edge[x]:
            num = len(Edge[y])
            Num[num-1].append(y)
            Edge[x].remove(y)
            Edge[y].remove(x) Num[i] Num i if Num[0]:
            Ans[Num[0][0]] = C[cnt]
            cnt += 1
         Num[0] Num 0 Ans[Num[0][0]] = C[cnt] Ans[Num[0][0]] Ans Num[0][0] [0] Num 0 0 C[cnt] C cnt cnt += 1 cnt 1 x = Num[i].pop() x Num[i].pop() [i].pop [i] Num i pop if Ans[x] != -1:
            continue
         Ans[x] != -1 Ans[x] Ans x -1 continue Ans[x] = C[cnt] Ans[x] Ans x C[cnt] C cnt ans += C[cnt] * i ans C[cnt] * i C[cnt] C cnt i cnt += 1 cnt 1 for y in Edge[x]:
            num = len(Edge[y])
            Num[num-1].append(y)
            Edge[x].remove(y)
            Edge[y].remove(x) y Edge[x] Edge x num = len(Edge[y]) num len(Edge[y]) len Edge[y] Edge y Num[num-1].append(y) [num-1].append [num-1] Num num-1 num 1 append y Edge[x].remove(y) [x].remove [x] Edge x remove y Edge[y].remove(x) [y].remove [y] Edge y remove x print(ans) print ans for i, a in enumerate(Ans):
    if i == N-1:
        print(a)
    else:
        print(a, end=' ') i a enumerate(Ans) enumerate Ans if i == N-1:
        print(a)
    else:
        print(a, end=' ') i == N-1 i N-1 N 1 print(a) print a print(a, end=' ') print a end=' ' end ' ' ans += C[cnt] * i C[cnt] * i ans a, b = map(int,input().split()) map(int,input().split()) a b = map(int,input().split()) map(int,input().split()) b cnt = 0 0 cnt b -= 1 1 b ans = 0 0 ans N = int(input()) int(input()) N cnt += 1 1 cnt x = Num[i].pop() Num[i].pop() x C = list(map(int, input().split())) list(map(int, input().split())) C Edge = [[]for _ in range(N)] [[]for _ in range(N)] Edge a -= 1 1 a Ans = [-1] * N [-1] * N Ans cnt += 1 1 cnt Num = [[] for _ in range(N)] [[] for _ in range(N)] Num num = len(Edge[y]) len(Edge[y]) num
N = int(input())

Edge = [[]for _ in range(N)]
Num = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(int,input().split())
    a -= 1
    b -= 1
    Edge[a].append(b)
    Edge[b].append(a)
for _ in range(N):
    Num[len(Edge[_])].append(_)
C = list(map(int, input().split()))
C.sort()
cnt = 0
Ans = [-1] * N
ans = 0
for i in range(1, N):
    if cnt == N:
        break
    while Num[i]:
        if Num[0]:
            Ans[Num[0][0]] = C[cnt]
            cnt += 1
        x = Num[i].pop()
        if Ans[x] != -1:
            continue
        Ans[x] = C[cnt]
        ans += C[cnt] * i
        cnt += 1
        for y in Edge[x]:
            num = len(Edge[y])
            Num[num-1].append(y)
            Edge[x].remove(y)
            Edge[y].remove(x)

print(ans)
for i, a in enumerate(Ans):
    if i == N-1:
        print(a)
    else:
        print(a, end=' ')
