# m-solutions2019D - Maximum Sum of Minimum
def bfs(start: int) -> None:
    q = [start]
    nodes[start] = C.pop()
    while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u)


def main():
    # adjacent edges should have close numbers as much as possible -> sort, bfs
    global T, C, nodes
    N, *ABC = map(int, open(0).read().split())
    AB, C = ABC[:-N], ABC[-N:]
    C.sort()
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
    score, nodes = sum(C) - C[-1], [0] * (N + 1)  # max of C will not be used
    for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
    bfs(start)
    print(score)
    print(*nodes[1:])


if __name__ == "__main__":
    main() def bfs(start: int) -> None:
    q = [start]
    nodes[start] = C.pop()
    while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) bfs q = [start] q [start] start nodes[start] = C.pop() nodes[start] nodes start C.pop() C.pop C pop while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) q v = q.pop(0) v q.pop(0) q.pop q pop 0 for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) u T[v] T v if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) not nodes[u] nodes[u] nodes u nodes[u] = C.pop() nodes[u] nodes u C.pop() C.pop C pop q.append(u) q.append q append u start: int start def main():
    # adjacent edges should have close numbers as much as possible -> sort, bfs
    global T, C, nodes
    N, *ABC = map(int, open(0).read().split())
    AB, C = ABC[:-N], ABC[-N:]
    C.sort()
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
    score, nodes = sum(C) - C[-1], [0] * (N + 1)  # max of C will not be used
    for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
    bfs(start)
    print(score)
    print(*nodes[1:]) main global T, C, nodes N, *ABC = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split *ABC ABC AB, C = ABC[:-N], ABC[-N:] AB ABC[:-N] ABC :-N -N N C ABC[-N:] ABC -N: -N N C.sort() C.sort C sort [] for _ in range(N + 1) for _ in range(N + 1) _ range(N + 1) range N + 1 N 1 for _ in range(N + 1) [] T = [[] for _ in range(N + 1)] T [[] for _ in range(N + 1)] for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
     v u zip(*[iter(AB)] * 2) zip *[iter(AB)] * 2 [iter(AB)] * 2 [iter(AB)] iter(AB) iter AB 2 T[v].append(u) [v].append [v] T v append u T[u].append(v) [u].append [u] T u append v score, nodes = sum(C) - C[-1], [0] * (N + 1) score sum(C) - C[-1] sum(C) sum C C[-1] C -1 nodes [0] * (N + 1) [0] 0 N + 1 N 1 for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
     i t enumerate(T[1:], 1) enumerate T[1:] T 1: 1 1 if len(t) == 1:
            start = i  # start from any node with degree 1
            break
     len(t) == 1 len(t) len t 1 start = i start i break bfs(start) bfs start print(score) print score print(*nodes[1:]) print *nodes[1:] nodes[1:] nodes 1: 1 if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    # adjacent edges should have close numbers as much as possible -> sort, bfs
    global T, C, nodes
    N, *ABC = map(int, open(0).read().split())
    AB, C = ABC[:-N], ABC[-N:]
    C.sort()
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
    score, nodes = sum(C) - C[-1], [0] * (N + 1)  # max of C will not be used
    for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
    bfs(start)
    print(score)
    print(*nodes[1:]) def main():
    # adjacent edges should have close numbers as much as possible -> sort, bfs
    global T, C, nodes
    N, *ABC = map(int, open(0).read().split())
    AB, C = ABC[:-N], ABC[-N:]
    C.sort()
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
    score, nodes = sum(C) - C[-1], [0] * (N + 1)  # max of C will not be used
    for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
    bfs(start)
    print(score)
    print(*nodes[1:]) main def bfs(start: int) -> None:
    q = [start]
    nodes[start] = C.pop()
    while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) def bfs(start: int) -> None:
    q = [start]
    nodes[start] = C.pop()
    while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u) bfs
# m-solutions2019D - Maximum Sum of Minimum
def bfs(start: int) -> None:
    q = [start]
    nodes[start] = C.pop()
    while q:
        v = q.pop(0)
        for u in T[v]:
            if not nodes[u]:
                nodes[u] = C.pop()
                q.append(u)


def main():
    # adjacent edges should have close numbers as much as possible -> sort, bfs
    global T, C, nodes
    N, *ABC = map(int, open(0).read().split())
    AB, C = ABC[:-N], ABC[-N:]
    C.sort()
    T = [[] for _ in range(N + 1)]
    for v, u in zip(*[iter(AB)] * 2):
        T[v].append(u), T[u].append(v)
    score, nodes = sum(C) - C[-1], [0] * (N + 1)  # max of C will not be used
    for i, t in enumerate(T[1:], 1):
        if len(t) == 1:
            start = i  # start from any node with degree 1
            break
    bfs(start)
    print(score)
    print(*nodes[1:])


if __name__ == "__main__":
    main()