def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(input())

    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    *c, = sorted(map(int, input().split()))

    num = [-1] * n
    curr = 0
    dfs(0)

    print(sum(c[:-1]))
    print(*num)


if __name__ == '__main__':
    main() def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(input())

    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    *c, = sorted(map(int, input().split()))

    num = [-1] * n
    curr = 0
    dfs(0)

    print(sum(c[:-1]))
    print(*num) main import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

     dfs nonlocal curr num[v] = 0 num[v] num v 0 for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
         u g[v] g v if num[u] >= 0:
                continue
             num[u] >= 0 num[u] num u 0 continue dfs(u) dfs u num[v] = c[curr] num[v] num v c[curr] c curr curr += 1 curr 1 v v n = int(input()) n int(input()) int input() input set() for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) set() set g = [set() for _ in range(n)] g [set() for _ in range(n)] for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
     _ range(n - 1) range n - 1 n 1 int(x) - 1 for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) - 1 int(x) int x 1 a, b = (int(x) - 1 for x in input().split()) a (int(x) - 1 for x in input().split()) b g[a].add(b) [a].add [a] g a add b g[b].add(a) [b].add [b] g b add a *c, = sorted(map(int, input().split())) *c c sorted(map(int, input().split())) sorted map(int, input().split()) map int input().split() ().split () input split num = [-1] * n num [-1] * n [-1] -1 n curr = 0 curr 0 dfs(0) dfs 0 print(sum(c[:-1])) print sum(c[:-1]) sum c[:-1] c :-1 -1 print(*num) print *num num if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(input())

    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    *c, = sorted(map(int, input().split()))

    num = [-1] * n
    curr = 0
    dfs(0)

    print(sum(c[:-1]))
    print(*num) def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(input())

    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    *c, = sorted(map(int, input().split()))

    num = [-1] * n
    curr = 0
    dfs(0)

    print(sum(c[:-1]))
    print(*num) main
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    def dfs(v):
        nonlocal curr

        num[v] = 0  # 0: visited, but not written yet

        for u in g[v]:
            if num[u] >= 0:
                continue
            dfs(u)
        num[v] = c[curr]
        curr += 1

    n = int(input())

    g = [set() for _ in range(n)]
    for _ in range(n - 1):
        a, b = (int(x) - 1 for x in input().split())
        g[a].add(b)
        g[b].add(a)
        # 出次数は、len(g[x])-1
        # 親を除いた残りが子
    *c, = sorted(map(int, input().split()))

    num = [-1] * n
    curr = 0
    dfs(0)

    print(sum(c[:-1]))
    print(*num)


if __name__ == '__main__':
    main()
