
def input_from_console():
    n = int(input())
    a_list, b_list = [], []
    for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
    c_list = list(map(int, input().split()))
    return n, a_list, b_list, c_list


def solve(n, a_list, b_list, c_list):
    edge = [[] for i in range(n)]  # 0 based list of list.
    for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
    c_list.sort(reverse=True)

    vertices = [None] * n
    vertices[0] = c_list.pop(0)

    graph_value = sum(c_list)  # edge should have the value except the maximum.

    search_path = []
    search_path.extend(edge[0])

    while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
    return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices)))


def main():
    n, a_list, b_list, c_list = input_from_console()
    print(solve(n, a_list, b_list, c_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main() def input_from_console():
    n = int(input())
    a_list, b_list = [], []
    for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
    c_list = list(map(int, input().split()))
    return n, a_list, b_list, c_list input_from_console n = int(input()) n int(input()) int input() input a_list, b_list = [], [] a_list [] b_list [] for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
     i range(n - 1) range n - 1 n 1 data = input() data input() input a, b = map(int, data.split()) a map(int, data.split()) map int data.split() data.split data split b if b > a:
            a, b = b, a  # swap smaller one.
         b > a b a a, b = b, a a b b a a_list.append(a) a_list.append a_list append a b_list.append(b) b_list.append b_list append b c_list = list(map(int, input().split())) c_list list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split return n, a_list, b_list, c_list return n, a_list, b_list, c_list n a_list b_list c_list def solve(n, a_list, b_list, c_list):
    edge = [[] for i in range(n)]  # 0 based list of list.
    for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
    c_list.sort(reverse=True)

    vertices = [None] * n
    vertices[0] = c_list.pop(0)

    graph_value = sum(c_list)  # edge should have the value except the maximum.

    search_path = []
    search_path.extend(edge[0])

    while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
    return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices))) solve [] for i in range(n) for i in range(n) i range(n) range n for i in range(n) [] edge = [[] for i in range(n)] edge [[] for i in range(n)] for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
     i range(len(a_list)) range len(a_list) len a_list a, b = a_list[i] - 1, b_list[i] - 1 a a_list[i] - 1 a_list[i] a_list i 1 b b_list[i] - 1 b_list[i] b_list i 1 edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a c_list.sort(reverse=True) c_list.sort c_list sort reverse=True reverse True vertices = [None] * n vertices [None] * n [None] None n vertices[0] = c_list.pop(0) vertices[0] vertices 0 c_list.pop(0) c_list.pop c_list pop 0 graph_value = sum(c_list) graph_value sum(c_list) sum c_list search_path = [] search_path [] search_path.extend(edge[0]) search_path.extend search_path extend edge[0] edge 0 while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
     search_path node = search_path.pop(0) node search_path.pop(0) search_path.pop search_path pop 0 if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
     vertices[node] is None vertices[node] vertices node None vertices[node] = c_list.pop(0) vertices[node] vertices node c_list.pop(0) c_list.pop c_list pop 0 search_path.extend(edge[node]) search_path.extend search_path extend edge[node] edge node return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices))) "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices))) "{:d}\n{}".format "{:d}\n{}" format graph_value ' '.join(map(str, vertices)) ' '.join ' ' join map(str, vertices) map str vertices n n a_list a_list b_list b_list c_list c_list def main():
    n, a_list, b_list, c_list = input_from_console()
    print(solve(n, a_list, b_list, c_list)) main n, a_list, b_list, c_list = input_from_console() n input_from_console() input_from_console a_list b_list c_list print(solve(n, a_list, b_list, c_list)) print solve(n, a_list, b_list, c_list) solve n a_list b_list c_list if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main() __name__ == "__main__" __name__ "__main__" import sys sys if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main() sys.gettrace() sys.gettrace sys gettrace check_cases() check_cases main() main def main():
    n, a_list, b_list, c_list = input_from_console()
    print(solve(n, a_list, b_list, c_list)) def main():
    n, a_list, b_list, c_list = input_from_console()
    print(solve(n, a_list, b_list, c_list)) main def input_from_console():
    n = int(input())
    a_list, b_list = [], []
    for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
    c_list = list(map(int, input().split()))
    return n, a_list, b_list, c_list def input_from_console():
    n = int(input())
    a_list, b_list = [], []
    for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
    c_list = list(map(int, input().split()))
    return n, a_list, b_list, c_list input_from_console def solve(n, a_list, b_list, c_list):
    edge = [[] for i in range(n)]  # 0 based list of list.
    for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
    c_list.sort(reverse=True)

    vertices = [None] * n
    vertices[0] = c_list.pop(0)

    graph_value = sum(c_list)  # edge should have the value except the maximum.

    search_path = []
    search_path.extend(edge[0])

    while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
    return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices))) def solve(n, a_list, b_list, c_list):
    edge = [[] for i in range(n)]  # 0 based list of list.
    for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
    c_list.sort(reverse=True)

    vertices = [None] * n
    vertices[0] = c_list.pop(0)

    graph_value = sum(c_list)  # edge should have the value except the maximum.

    search_path = []
    search_path.extend(edge[0])

    while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
    return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices))) solve

def input_from_console():
    n = int(input())
    a_list, b_list = [], []
    for i in range(n - 1):
        data = input()
        a, b = map(int, data.split())
        if b > a:
            a, b = b, a  # swap smaller one.
        a_list.append(a)
        b_list.append(b)
    c_list = list(map(int, input().split()))
    return n, a_list, b_list, c_list


def solve(n, a_list, b_list, c_list):
    edge = [[] for i in range(n)]  # 0 based list of list.
    for i in range(len(a_list)):
        a, b = a_list[i] - 1, b_list[i] - 1  # convert to 0 based index.
        edge[a].append(b)
        edge[b].append(a)
    c_list.sort(reverse=True)

    vertices = [None] * n
    vertices[0] = c_list.pop(0)

    graph_value = sum(c_list)  # edge should have the value except the maximum.

    search_path = []
    search_path.extend(edge[0])

    while search_path:
        node = search_path.pop(0)
        if vertices[node] is None:
            vertices[node] = c_list.pop(0)
            search_path.extend(edge[node])
    return "{:d}\n{}".format(graph_value, ' '.join(map(str, vertices)))


def main():
    n, a_list, b_list, c_list = input_from_console()
    print(solve(n, a_list, b_list, c_list))


if __name__ == "__main__":
    import sys

    if sys.gettrace():  # Check if the python interpreter is in debug mode or not.
        check_cases()
    else:
        main()
