Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

e = [sorted(list(map(lambda x:int(x)-1,input().split()))) for i in range(N-1)]
e.sort()
c = list(map(int,input().split()))
c.sort()
c.reverse()
ans = sum(c)-c[0]
d = [0]*N
k = 0
d[0] = str(c[k])
k += 1
while e:
    a,b = e.pop(0)
    if d[a]==0 and d[b]!=0:
        d[a] = str(c[k])
        k+=1
    elif d[a]!=0 and d[b]==0:
        d[b] = str(c[k])
        k+=1
    else:
        e.append((a,b))
print(ans)
print(' '.join(d))

Fixed submission:
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))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

e = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
c = list(map(int,input().split()))
c.sort()
c.reverse()
ans = sum(c)-c[0]
d = [0]*N
k = 0
d[0] = str(c[k])
k += 1
while e:
    a,b = e.pop(0)
    if d[a]==0 and d[b]!=0:
        d[a] = str(c[k])
        k+=1
    elif d[a]!=0 and d[b]==0:
        d[b] = str(c[k])
        k+=1
    else:
        e.append((a,b))
print(ans)
print(' '.join(d))

Fixed submission:
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))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

e = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(N-1)]
c = list(map(int,input().split()))
c.sort()
c.reverse()
ans = sum(c)-c[0]
d = [0]*N
k = 0
d[0] = str(c[k])
k += 1
for _ in range(N-1):
    for i in range(N-1):
        a,b = e[i]
        if d[a]==0 and d[b]!=0:
            d[a] = str(c[k])
            k+=1
        elif d[a]!=0 and d[b]==0:
            d[b] = str(c[k])
            k+=1
print(ans)
print(' '.join(d))

Fixed submission:
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))