import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
res = 0


def resolve():
    def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    C = list(map(int, input().split()))
    C.sort()

    col = [0] * n
    col[0] = C.pop()
    dfs(0, -1)
    print(res)
    print(*col)


if __name__ == '__main__':
    resolve() import sys sys sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 f_inf = float('inf') f_inf float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 res = 0 res 0 def resolve():
    def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    C = list(map(int, input().split()))
    C.sort()

    col = [0] * n
    col[0] = C.pop()
    dfs(0, -1)
    print(res)
    print(*col) resolve def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

     dfs global res for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

     u edge[v] edge v if u == p:
                continue
             u == p u p continue col[u] = C.pop() col[u] col u C.pop() C.pop C pop res += min(col[v], col[u]) res min(col[v], col[u]) min col[v] col v col[u] col u dfs(u, v) dfs u v v v p p n = int(input()) n int(input()) int input() input [] for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) [] edge = [[] for _ in range(n)] edge [[] for _ in range(n)] for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
     _ 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 edge[a - 1].append(b - 1) [a - 1].append [a - 1] edge a - 1 a 1 append b - 1 b 1 edge[b - 1].append(a - 1) [b - 1].append [b - 1] edge b - 1 b 1 append a - 1 a 1 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 col = [0] * n col [0] * n [0] 0 n col[0] = C.pop() col[0] col 0 C.pop() C.pop C pop dfs(0, -1) dfs 0 -1 print(res) print res print(*col) print *col col if __name__ == '__main__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve f_inf = float('inf') float('inf') f_inf def resolve():
    def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    C = list(map(int, input().split()))
    C.sort()

    col = [0] * n
    col[0] = C.pop()
    dfs(0, -1)
    print(res)
    print(*col) def resolve():
    def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    C = list(map(int, input().split()))
    C.sort()

    col = [0] * n
    col[0] = C.pop()
    dfs(0, -1)
    print(res)
    print(*col) resolve mod = 10 ** 9 + 7 10 ** 9 + 7 mod res = 0 0 res
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7
res = 0


def resolve():
    def dfs(v, p):
        global res
        for u in edge[v]:
            if u == p:
                continue
            col[u] = C.pop()
            res += min(col[v], col[u])
            dfs(u, v)

    n = int(input())
    edge = [[] for _ in range(n)]
    for _ in range(n - 1):
        a, b = map(int, input().split())
        edge[a - 1].append(b - 1)
        edge[b - 1].append(a - 1)
    C = list(map(int, input().split()))
    C.sort()

    col = [0] * n
    col[0] = C.pop()
    dfs(0, -1)
    print(res)
    print(*col)


if __name__ == '__main__':
    resolve()
