import sys

sys.setrecursionlimit(110000)


def inpl():
    return list(map(int, input().split()))


class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return


N = int(input())
uf = UnionFind(N)
AB = [[0, 0] for i in range(N - 1)]

for i in range(N - 1):
    a, b = inpl()
    a -= 1
    b -= 1
    AB[i][0] = a
    AB[i][1] = b
    uf.adj[a].append(b)
    uf.adj[b].append(a)

C = inpl()

for a, b in AB:
    uf.union(a, b)

root_num = uf.find_root()
uf.hop[root_num] = 0
uf.dfs(root_num)

dic2 = sorted(uf.hop.items(), key=lambda x: x[1], reverse=True)

ans = [0 for i in range(N)]
for i, k in enumerate(dic2):
    ans[k[0]] = i

C.sort()

print(sum(C[:-1]))
for i in range(N):
    if i != 0:
        print(" ", end="")
    print(C[ans[i]], end="")
print("") import sys sys sys.setrecursionlimit(110000) sys.setrecursionlimit sys setrecursionlimit 110000 def inpl():
    return list(map(int, input().split())) inpl return list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return UnionFind def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

     __init__ i for i in range(N) for i in range(N) i range(N) range N for i in range(N) i self.par = [i for i in range(N)] self.par self par [i for i in range(N)] self.N = N self.N self N N i: -1 for i in range(N) for i in range(N) i range(N) range N for i in range(N) i -1 self.hop = {i: -1 for i in range(N)} self.hop self hop {i: -1 for i in range(N)} [] for i in range(N) for i in range(N) i range(N) range N for i in range(N) [] self.adj = [[] for i in range(N)] self.adj self adj [[] for i in range(N)] return self self N N def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

     root if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

     self.par[x] == x self.par[x] self.par self par x x return (x) x self.par[x] = self.root(self.par[x]) self.par[x] self.par self par x self.root(self.par[x]) self.root self root self.par[x] self.par self par x return self.par[x] self.par[x] self.par self par x self self x x def same(self, x, y):
        return (self.root(x) == self.root(y))

     same return (self.root(x) == self.root(y)) self.root(x) == self.root(y) self.root(x) self.root self root x self.root(y) self.root self root y self self x x y y def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

     union x = self.root(x) x self.root(x) self.root self root x y = self.root(y) y self.root(y) self.root self root y if (x == y):
            return False

         x == y x y return False False self.par[x] = min(x, y) self.par[x] self.par self par x min(x, y) min x y self.par[y] = min(x, y) self.par[y] self.par self par y min(x, y) min x y return True True self self x x y y def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

     find_root for i in range(self.N):
            if i == self.root(i):
                return i
         i range(self.N) range self.N self N if i == self.root(i):
                return i
         i == self.root(i) i self.root(i) self.root self root i return i i return self self def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return dfs for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
         nh self.adj[i] self.adj self adj i if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
         self.hop[nh] != -1 self.hop[nh] self.hop self hop nh -1 continue self.hop[nh] = self.hop[i] + 1 self.hop[nh] self.hop self hop nh self.hop[i] + 1 self.hop[i] self.hop self hop i 1 self.dfs(nh) self.dfs self dfs nh return self self i i N = int(input()) N int(input()) int input() input uf = UnionFind(N) uf UnionFind(N) UnionFind N [0, 0] for i in range(N - 1) for i in range(N - 1) i range(N - 1) range N - 1 N 1 for i in range(N - 1) [0, 0] 0 0 AB = [[0, 0] for i in range(N - 1)] AB [[0, 0] for i in range(N - 1)] for i in range(N - 1):
    a, b = inpl()
    a -= 1
    b -= 1
    AB[i][0] = a
    AB[i][1] = b
    uf.adj[a].append(b)
    uf.adj[b].append(a) i range(N - 1) range N - 1 N 1 a, b = inpl() a inpl() inpl b a -= 1 a 1 b -= 1 b 1 AB[i][0] = a AB[i][0] [i] AB i 0 a AB[i][1] = b AB[i][1] [i] AB i 1 b uf.adj[a].append(b) uf.adj[a].append uf.adj[a] uf.adj uf adj a append b uf.adj[b].append(a) uf.adj[b].append uf.adj[b] uf.adj uf adj b append a C = inpl() C inpl() inpl for a, b in AB:
    uf.union(a, b) a b AB uf.union(a, b) uf.union uf union a b root_num = uf.find_root() root_num uf.find_root() uf.find_root uf find_root uf.hop[root_num] = 0 uf.hop[root_num] uf.hop uf hop root_num 0 uf.dfs(root_num) uf.dfs uf dfs root_num dic2 = sorted(uf.hop.items(), key=lambda x: x[1], reverse=True) dic2 sorted(uf.hop.items(), key=lambda x: x[1], reverse=True) sorted uf.hop.items() uf.hop.items uf.hop uf hop items key=lambda x: x[1] key lambda x: x[1] x[1] x 1 x reverse=True reverse True 0 for i in range(N) for i in range(N) i range(N) range N for i in range(N) 0 ans = [0 for i in range(N)] ans [0 for i in range(N)] for i, k in enumerate(dic2):
    ans[k[0]] = i i k enumerate(dic2) enumerate dic2 ans[k[0]] = i ans[k[0]] ans k[0] k 0 i C.sort() C.sort C sort print(sum(C[:-1])) print sum(C[:-1]) sum C[:-1] C :-1 -1 for i in range(N):
    if i != 0:
        print(" ", end="")
    print(C[ans[i]], end="") i range(N) range N if i != 0:
        print(" ", end="")
     i != 0 i 0 print(" ", end="") print " " end="" end "" print(C[ans[i]], end="") print C[ans[i]] C ans[i] ans i end="" end "" print("") print "" C = inpl() inpl() C ans = [0 for i in range(N)] [0 for i in range(N)] ans a, b = inpl() inpl() a def inpl():
    return list(map(int, input().split())) def inpl():
    return list(map(int, input().split())) inpl class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return UnionFind N = int(input()) int(input()) N root_num = uf.find_root() uf.find_root() root_num dic2 = sorted(uf.hop.items(), key=lambda x: x[1], reverse=True) sorted(uf.hop.items(), key=lambda x: x[1], reverse=True) dic2 b -= 1 1 b AB = [[0, 0] for i in range(N - 1)] [[0, 0] for i in range(N - 1)] AB a -= 1 1 a b = inpl() inpl() b uf = UnionFind(N) UnionFind(N) uf
import sys

sys.setrecursionlimit(110000)


def inpl():
    return list(map(int, input().split()))


class UnionFind:
    def __init__(self, N):
        # par = parent
        self.par = [i for i in range(N)]
        self.N = N
        self.hop = {i: -1 for i in range(N)}
        self.adj = [[] for i in range(N)]
        return

    def root(self, x):
        if self.par[x] == x:
            return (x)
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def same(self, x, y):
        return (self.root(x) == self.root(y))

    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if (x == y):
            return False

        self.par[x] = min(x, y)
        self.par[y] = min(x, y)
        return True

    def find_root(self):
        for i in range(self.N):
            if i == self.root(i):
                return i
        return

    def dfs(self, i):
        for nh in self.adj[i]:
            if self.hop[nh] != -1:
                continue
            else:
                self.hop[nh] = self.hop[i] + 1
                self.dfs(nh)
        return


N = int(input())
uf = UnionFind(N)
AB = [[0, 0] for i in range(N - 1)]

for i in range(N - 1):
    a, b = inpl()
    a -= 1
    b -= 1
    AB[i][0] = a
    AB[i][1] = b
    uf.adj[a].append(b)
    uf.adj[b].append(a)

C = inpl()

for a, b in AB:
    uf.union(a, b)

root_num = uf.find_root()
uf.hop[root_num] = 0
uf.dfs(root_num)

dic2 = sorted(uf.hop.items(), key=lambda x: x[1], reverse=True)

ans = [0 for i in range(N)]
for i, k in enumerate(dic2):
    ans[k[0]] = i

C.sort()

print(sum(C[:-1]))
for i in range(N):
    if i != 0:
        print(" ", end="")
    print(C[ans[i]], end="")
print("")
