n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
L = list(map(int,input().split()))
L.sort()
used = [0]*n
ans = [0]*n
used[0] = 1
ans[0] = L[n-1]
next = graph[0]
cur = n-2
while len(next) > 0:
    t = next.pop()
    ans[t] = L[cur]
    cur -= 1
    used[t] = 1
    for i in range(len(graph[t])):
        if used[graph[t][i]] != 1:
            next.append(graph[t][i])
print(sum(L)-L[n-1])
print(' '.join(map(str,ans))) 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) [] graph = [[] for i in range(n)] graph [[] for i in range(n)] for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1) 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 graph[a-1].append(b-1) [a-1].append [a-1] graph a-1 a 1 append b-1 b 1 graph[b-1].append(a-1) [b-1].append [b-1] graph b-1 b 1 append a-1 a 1 L = list(map(int,input().split())) L list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split L.sort() L.sort L sort used = [0]*n used [0]*n [0] 0 n ans = [0]*n ans [0]*n [0] 0 n used[0] = 1 used[0] used 0 1 ans[0] = L[n-1] ans[0] ans 0 L[n-1] L n-1 n 1 next = graph[0] next graph[0] graph 0 cur = n-2 cur n-2 n 2 while len(next) > 0:
    t = next.pop()
    ans[t] = L[cur]
    cur -= 1
    used[t] = 1
    for i in range(len(graph[t])):
        if used[graph[t][i]] != 1:
            next.append(graph[t][i]) len(next) > 0 len(next) len next 0 t = next.pop() t next.pop() next.pop next pop ans[t] = L[cur] ans[t] ans t L[cur] L cur cur -= 1 cur 1 used[t] = 1 used[t] used t 1 for i in range(len(graph[t])):
        if used[graph[t][i]] != 1:
            next.append(graph[t][i]) i range(len(graph[t])) range len(graph[t]) len graph[t] graph t if used[graph[t][i]] != 1:
            next.append(graph[t][i]) used[graph[t][i]] != 1 used[graph[t][i]] used graph[t][i] [t] graph t i 1 next.append(graph[t][i]) next.append next append graph[t][i] [t] graph t i print(sum(L)-L[n-1]) print sum(L)-L[n-1] sum(L) sum L L[n-1] L n-1 n 1 print(' '.join(map(str,ans))) print ' '.join(map(str,ans)) ' '.join ' ' join map(str,ans) map str ans b = map(int,input().split()) map(int,input().split()) b L = list(map(int,input().split())) list(map(int,input().split())) L cur = n-2 n-2 cur n = int(input()) int(input()) n next = graph[0] graph[0] next a,b = map(int,input().split()) map(int,input().split()) a t = next.pop() next.pop() t used = [0]*n [0]*n used graph = [[] for i in range(n)] [[] for i in range(n)] graph ans = [0]*n [0]*n ans cur -= 1 1 cur
n = int(input())
graph = [[] for i in range(n)]
for i in range(n-1):
    a,b = map(int,input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)
L = list(map(int,input().split()))
L.sort()
used = [0]*n
ans = [0]*n
used[0] = 1
ans[0] = L[n-1]
next = graph[0]
cur = n-2
while len(next) > 0:
    t = next.pop()
    ans[t] = L[cur]
    cur -= 1
    used[t] = 1
    for i in range(len(graph[t])):
        if used[graph[t][i]] != 1:
            next.append(graph[t][i])
print(sum(L)-L[n-1])
print(' '.join(map(str,ans)))
