n = int(input())
ab = [list(map(int, input().split())) for _ in range(n-1)]
c = list(map(int,input().split()))

edge_num = [0] * (n+1)
edge = {}
for i in range(1,n+1):
    edge[i] = set()

for a,b in ab:
    edge_num[a] += 1
    edge_num[b] += 1
    edge[a].add(b)
    edge[b].add(a)

edge_num2 = {}
for i in range(max(edge_num) + 1):
    edge_num2[i] = set()

for i in range(1,n+1):
    edge_num2[edge_num[i]].add(i)

c = sorted(c)
c_num = 0
ans = [0] * (n+1)
while(len(edge_num2[1]) > 0):
    tmp = edge_num2[1].pop()
    ans[tmp] = c[c_num]
    c_num += 1

    neigh = edge[tmp].pop()
    edge_num2[edge_num[neigh]].discard(neigh)
    edge_num[neigh] -= 1
    edge_num2[edge_num[neigh]].add(neigh)
    edge[neigh].discard(tmp)
    if(edge_num[neigh] == 0):
        ans[neigh] = c[c_num]
        break

print(sum(c) - max(c))
print(' '.join(map(str, ans[1:]))) n = int(input()) n int(input()) int input() input list(map(int, input().split())) for _ in range(n-1) for _ in range(n-1) _ range(n-1) range n-1 n 1 for _ in range(n-1) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ab = [list(map(int, input().split())) for _ in range(n-1)] ab [list(map(int, input().split())) for _ in range(n-1)] c = list(map(int,input().split())) c list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split edge_num = [0] * (n+1) edge_num [0] * (n+1) [0] 0 n+1 n 1 edge = {} edge {} for i in range(1,n+1):
    edge[i] = set() i range(1,n+1) range 1 n+1 n 1 edge[i] = set() edge[i] edge i set() set for a,b in ab:
    edge_num[a] += 1
    edge_num[b] += 1
    edge[a].add(b)
    edge[b].add(a) a b ab edge_num[a] += 1 edge_num[a] edge_num a 1 edge_num[b] += 1 edge_num[b] edge_num b 1 edge[a].add(b) [a].add [a] edge a add b edge[b].add(a) [b].add [b] edge b add a edge_num2 = {} edge_num2 {} for i in range(max(edge_num) + 1):
    edge_num2[i] = set() i range(max(edge_num) + 1) range max(edge_num) + 1 max(edge_num) max edge_num 1 edge_num2[i] = set() edge_num2[i] edge_num2 i set() set for i in range(1,n+1):
    edge_num2[edge_num[i]].add(i) i range(1,n+1) range 1 n+1 n 1 edge_num2[edge_num[i]].add(i) [edge_num[i]].add [edge_num[i]] edge_num2 edge_num[i] edge_num i add i c = sorted(c) c sorted(c) sorted c c_num = 0 c_num 0 ans = [0] * (n+1) ans [0] * (n+1) [0] 0 n+1 n 1 while(len(edge_num2[1]) > 0):
    tmp = edge_num2[1].pop()
    ans[tmp] = c[c_num]
    c_num += 1

    neigh = edge[tmp].pop()
    edge_num2[edge_num[neigh]].discard(neigh)
    edge_num[neigh] -= 1
    edge_num2[edge_num[neigh]].add(neigh)
    edge[neigh].discard(tmp)
    if(edge_num[neigh] == 0):
        ans[neigh] = c[c_num]
        break len(edge_num2[1]) > 0 len(edge_num2[1]) len edge_num2[1] edge_num2 1 0 tmp = edge_num2[1].pop() tmp edge_num2[1].pop() [1].pop [1] edge_num2 1 pop ans[tmp] = c[c_num] ans[tmp] ans tmp c[c_num] c c_num c_num += 1 c_num 1 neigh = edge[tmp].pop() neigh edge[tmp].pop() [tmp].pop [tmp] edge tmp pop edge_num2[edge_num[neigh]].discard(neigh) [edge_num[neigh]].discard [edge_num[neigh]] edge_num2 edge_num[neigh] edge_num neigh discard neigh edge_num[neigh] -= 1 edge_num[neigh] edge_num neigh 1 edge_num2[edge_num[neigh]].add(neigh) [edge_num[neigh]].add [edge_num[neigh]] edge_num2 edge_num[neigh] edge_num neigh add neigh edge[neigh].discard(tmp) [neigh].discard [neigh] edge neigh discard tmp if(edge_num[neigh] == 0):
        ans[neigh] = c[c_num]
        break edge_num[neigh] == 0 edge_num[neigh] edge_num neigh 0 ans[neigh] = c[c_num] ans[neigh] ans neigh c[c_num] c c_num break print(sum(c) - max(c)) print sum(c) - max(c) sum(c) sum c max(c) max c print(' '.join(map(str, ans[1:]))) print ' '.join(map(str, ans[1:])) ' '.join ' ' join map(str, ans[1:]) map str ans[1:] ans 1: 1 ab = [list(map(int, input().split())) for _ in range(n-1)] [list(map(int, input().split())) for _ in range(n-1)] ab edge = {} {} edge ans = [0] * (n+1) [0] * (n+1) ans edge_num = [0] * (n+1) [0] * (n+1) edge_num c = sorted(c) sorted(c) c n = int(input()) int(input()) n c_num = 0 0 c_num edge_num2 = {} {} edge_num2 c = list(map(int,input().split())) list(map(int,input().split())) c tmp = edge_num2[1].pop() edge_num2[1].pop() tmp c_num += 1 1 c_num neigh = edge[tmp].pop() edge[tmp].pop() neigh
n = int(input())
ab = [list(map(int, input().split())) for _ in range(n-1)]
c = list(map(int,input().split()))

edge_num = [0] * (n+1)
edge = {}
for i in range(1,n+1):
    edge[i] = set()

for a,b in ab:
    edge_num[a] += 1
    edge_num[b] += 1
    edge[a].add(b)
    edge[b].add(a)

edge_num2 = {}
for i in range(max(edge_num) + 1):
    edge_num2[i] = set()

for i in range(1,n+1):
    edge_num2[edge_num[i]].add(i)

c = sorted(c)
c_num = 0
ans = [0] * (n+1)
while(len(edge_num2[1]) > 0):
    tmp = edge_num2[1].pop()
    ans[tmp] = c[c_num]
    c_num += 1

    neigh = edge[tmp].pop()
    edge_num2[edge_num[neigh]].discard(neigh)
    edge_num[neigh] -= 1
    edge_num2[edge_num[neigh]].add(neigh)
    edge[neigh].discard(tmp)
    if(edge_num[neigh] == 0):
        ans[neigh] = c[c_num]
        break

print(sum(c) - max(c))
print(' '.join(map(str, ans[1:])))