from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist

N = int(input())

e = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append((1,b))
    e[b].append((1,a))

c = list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
d = dijkstra(e,N,0)
d = [(d[i],i) for i in range(N)]
d.sort()
d = [(d[i][0],d[i][1],c[-i-1]) for i in range(N)]
d.sort(key=lambda x:x[1])
ans = [str(d[i][2]) for i in range(N)]
print(' '.join(ans)) from heapq import heappush, heappop def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist dijkstra inf = float('inf') inf float('inf') float 'inf' dist = [inf]*node dist [inf]*node [inf] inf node dist[start] = 0 dist[start] dist start 0 heap = [(0,start)] heap [(0,start)] (0,start) 0 start while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
     heap cost,thisNode = heappop(heap) cost heappop(heap) heappop heap thisNode for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
     NextCost NextNode graph[thisNode] graph thisNode dist_cand = dist[thisNode]+NextCost dist_cand dist[thisNode]+NextCost dist[thisNode] dist thisNode NextCost if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
     dist_cand < dist[NextNode] dist_cand dist[NextNode] dist NextNode dist[NextNode] = dist_cand dist[NextNode] dist NextNode dist_cand heappush(heap,(dist[NextNode],NextNode)) heappush heap (dist[NextNode],NextNode) dist[NextNode] dist NextNode NextNode return dist dist graph:list graph node:int node start:int start 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) [] e = [[] for i in range(N)] e [[] for i in range(N)] for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append((1,b))
    e[b].append((1,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 e[a].append((1,b)) [a].append [a] e a append (1,b) 1 b e[b].append((1,a)) [b].append [b] e b append (1,a) 1 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() c.sort c sort print(sum(c)-c[-1]) print sum(c)-c[-1] sum(c) sum c c[-1] c -1 d = dijkstra(e,N,0) d dijkstra(e,N,0) dijkstra e N 0 (d[i],i) for i in range(N) for i in range(N) i range(N) range N for i in range(N) (d[i],i) d[i] d i i d = [(d[i],i) for i in range(N)] d [(d[i],i) for i in range(N)] d.sort() d.sort d sort (d[i][0],d[i][1],c[-i-1]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) (d[i][0],d[i][1],c[-i-1]) d[i][0] [i] d i 0 d[i][1] [i] d i 1 c[-i-1] c -i-1 -i i 1 d = [(d[i][0],d[i][1],c[-i-1]) for i in range(N)] d [(d[i][0],d[i][1],c[-i-1]) for i in range(N)] d.sort(key=lambda x:x[1]) d.sort d sort key=lambda x:x[1] key lambda x:x[1] x[1] x 1 x str(d[i][2]) for i in range(N) for i in range(N) i range(N) range N for i in range(N) str(d[i][2]) str d[i][2] [i] d i 2 ans = [str(d[i][2]) for i in range(N)] ans [str(d[i][2]) for i in range(N)] print(' '.join(ans)) print ' '.join(ans) ' '.join ' ' join ans d = [(d[i][0],d[i][1],c[-i-1]) for i in range(N)] [(d[i][0],d[i][1],c[-i-1]) for i in range(N)] d ans = [str(d[i][2]) for i in range(N)] [str(d[i][2]) for i in range(N)] ans a -= 1 1 a b -= 1 1 b N = int(input()) int(input()) N d = [(d[i],i) for i in range(N)] [(d[i],i) for i in range(N)] d def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist dijkstra d = dijkstra(e,N,0) dijkstra(e,N,0) d e = [[] for i in range(N)] [[] for i in range(N)] e c = list(map(int,input().split())) list(map(int,input().split())) c a,b = map(int,input().split()) map(int,input().split()) a b = map(int,input().split()) map(int,input().split()) b
from heapq import heappush, heappop
def dijkstra(graph:list, node:int, start:int) -> list:
    # graph[node] = [(cost, to)]
    inf = float('inf')
    dist = [inf]*node

    dist[start] = 0
    heap = [(0,start)]
    while heap:
        cost,thisNode = heappop(heap)
        for NextCost,NextNode in graph[thisNode]:
            dist_cand = dist[thisNode]+NextCost
            if dist_cand < dist[NextNode]:
                dist[NextNode] = dist_cand
                heappush(heap,(dist[NextNode],NextNode))
    return dist

N = int(input())

e = [[] for i in range(N)]
for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append((1,b))
    e[b].append((1,a))

c = list(map(int,input().split()))
c.sort()
print(sum(c)-c[-1])
d = dijkstra(e,N,0)
d = [(d[i],i) for i in range(N)]
d.sort()
d = [(d[i][0],d[i][1],c[-i-1]) for i in range(N)]
d.sort(key=lambda x:x[1])
ans = [str(d[i][2]) for i in range(N)]
print(' '.join(ans))