#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


class DGraph:
    def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

    def __len__(self):
        return len(self._vertice)

    def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

    def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

    def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf'))

class Graph(DGraph):
    def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

    def transpose(self):
        return self



def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)]
    g = Graph(N, edges)
    s = [-1] * N

    c = deque(sorted(c, reverse=True))
    q = deque([0])
    while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

    ret = 0
    for b, e, _ in g.edges():
        ret += min(s[b], s[e])
    ret //= 2
    return '\n'.join([str(ret), ' '.join([str(n) for n in s])])

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, a, b, c)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main() import sys sys from collections.abc import Iterable from math import * from itertools import * from collections import * from functools import * from operator import * try:
    from math import gcd
except Exception:
    from fractions import gcd except Exception:
    from fractions import gcd from fractions import gcd from math import gcd class DGraph:
    def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

    def __len__(self):
        return len(self._vertice)

    def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

    def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

    def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf')) DGraph def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

     __init__ {} for _ in range(n) for _ in range(n) _ range(n) range n for _ in range(n) {} self._vertice = [{} for _ in range(n)] self._vertice self _vertice [{} for _ in range(n)] for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

     e edges self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1 self._vertice[e[0]][e[1]] self._vertice[e[0]] self._vertice self _vertice e[0] e 0 e[1] e 1 e[2] if len(e) == 3 else 1 len(e) == 3 len(e) len e 3 e[2] e 2 1 self self n n edges edges def __len__(self):
        return len(self._vertice)

     __len__ return len(self._vertice) len(self._vertice) len self._vertice self _vertice self self def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

     edges srcs = range(len(self)) if n is None else [n] srcs range(len(self)) if n is None else [n] n is None n None range(len(self)) range len(self) len self [n] n [(s, d, c) for d, c in self._vertice[s].items()] for s in srcs for s in srcs s srcs for s in srcs [(s, d, c) for d, c in self._vertice[s].items()] return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], []) sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], []) sum [[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs] [] self self n=None n None def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

     transpose (t, f, c) for f, t, c in self.edges() for f, t, c in self.edges() f t c self.edges() self.edges self edges for f, t, c in self.edges() (t, f, c) t f c tedges = [(t, f, c) for f, t, c in self.edges()] tedges [(t, f, c) for f, t, c in self.edges()] return DGraph(len(self), tedges) DGraph(len(self), tedges) DGraph len(self) len self tedges self self def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf')) cost return self._vertice[src].get(dst, float('inf')) self._vertice[src].get(dst, float('inf')) self._vertice[src].get self._vertice[src] self._vertice self _vertice src get dst float('inf') float 'inf' self self src src dst dst class Graph(DGraph):
    def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

    def transpose(self):
        return self Graph def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

     __init__ (d, s, c) for s, d, c in edges for s, d, c in edges s d c edges for s, d, c in edges (d, s, c) d s c nd_edges = edges + [(d, s, c) for s, d, c in edges] nd_edges edges + [(d, s, c) for s, d, c in edges] edges [(d, s, c) for s, d, c in edges] super().__init__(n, nd_edges) ().__init__ () super __init__ n nd_edges self self n n edges edges def transpose(self):
        return self transpose return self self self self DGraph def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)]
    g = Graph(N, edges)
    s = [-1] * N

    c = deque(sorted(c, reverse=True))
    q = deque([0])
    while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

    ret = 0
    for b, e, _ in g.edges():
        ret += min(s[b], s[e])
    ret //= 2
    return '\n'.join([str(ret), ' '.join([str(n) for n in s])]) solve (ca - 1, cb - 1, 0) for ca, cb in zip(a, b) for ca, cb in zip(a, b) ca cb zip(a, b) zip a b for ca, cb in zip(a, b) (ca - 1, cb - 1, 0) ca - 1 ca 1 cb - 1 cb 1 0 edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)] edges [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)] g = Graph(N, edges) g Graph(N, edges) Graph N edges s = [-1] * N s [-1] * N [-1] -1 N c = deque(sorted(c, reverse=True)) c deque(sorted(c, reverse=True)) deque sorted(c, reverse=True) sorted c reverse=True reverse True q = deque([0]) q deque([0]) deque [0] 0 while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

     0 < len(q) 0 len(q) len q n = q.popleft() n q.popleft() q.popleft q popleft if s[n] != -1:
            continue
         s[n] != -1 s[n] s n -1 continue s[n] = c.popleft() s[n] s n c.popleft() c.popleft c popleft for _, d, _ in g.edges(n):
            q.append(d)

     _ d _ g.edges(n) g.edges g edges n q.append(d) q.append q append d ret = 0 ret 0 for b, e, _ in g.edges():
        ret += min(s[b], s[e])
     b e _ g.edges() g.edges g edges ret += min(s[b], s[e]) ret min(s[b], s[e]) min s[b] s b s[e] s e ret //= 2 ret 2 str(n) for n in s for n in s n s for n in s str(n) str n return '\n'.join([str(ret), ' '.join([str(n) for n in s])]) '\n'.join([str(ret), ' '.join([str(n) for n in s])]) '\n'.join '\n' join [str(ret), ' '.join([str(n) for n in s])] str(ret) str ret ' '.join([str(n) for n in s]) ' '.join ' ' join [str(n) for n in s] N: int N a: "List[int]" a b: "List[int]" b c: "List[int]" c def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, a, b, c)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result) main def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
     iterate_tokens for line in sys.stdin:
            for word in line.split():
                yield word
     line sys.stdin sys stdin for word in line.split():
                yield word
     word line.split() line.split line split yield word word tokens = iterate_tokens() tokens iterate_tokens() iterate_tokens N = int(next(tokens)) N int(next(tokens)) int next(tokens) next tokens a = [int()] * (N-1) a [int()] * (N-1) [int()] int() int N-1 N 1 b = [int()] * (N-1) b [int()] * (N-1) [int()] int() int N-1 N 1 for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
     i range(N-1) range N-1 N 1 a[i] = int(next(tokens)) a[i] a i int(next(tokens)) int next(tokens) next tokens b[i] = int(next(tokens)) b[i] b i int(next(tokens)) int next(tokens) next tokens int(next(tokens)) for _ in range(N) for _ in range(N) _ range(N) range N for _ in range(N) int(next(tokens)) int next(tokens) next tokens c = [ int(next(tokens)) for _ in range(N) ] c [ int(next(tokens)) for _ in range(N) ] result = solve(N, a, b, c) result solve(N, a, b, c) solve N a b c if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
     isinstance(result, Iterable) and not isinstance(result, str) isinstance(result, Iterable) isinstance result Iterable not isinstance(result, str) isinstance(result, str) isinstance result str str(v) for v in result for v in result v result for v in result str(v) str v result = '\n'.join([str(v) for v in result]) result '\n'.join([str(v) for v in result]) '\n'.join '\n' join [str(v) for v in result] print(result) print result if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main class DGraph:
    def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

    def __len__(self):
        return len(self._vertice)

    def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

    def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

    def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf')) class DGraph:
    def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

    def __len__(self):
        return len(self._vertice)

    def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

    def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

    def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf')) DGraph def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)]
    g = Graph(N, edges)
    s = [-1] * N

    c = deque(sorted(c, reverse=True))
    q = deque([0])
    while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

    ret = 0
    for b, e, _ in g.edges():
        ret += min(s[b], s[e])
    ret //= 2
    return '\n'.join([str(ret), ' '.join([str(n) for n in s])]) def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)]
    g = Graph(N, edges)
    s = [-1] * N

    c = deque(sorted(c, reverse=True))
    q = deque([0])
    while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

    ret = 0
    for b, e, _ in g.edges():
        ret += min(s[b], s[e])
    ret //= 2
    return '\n'.join([str(ret), ' '.join([str(n) for n in s])]) solve def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, a, b, c)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result) def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, a, b, c)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result) main class Graph(DGraph):
    def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

    def transpose(self):
        return self class Graph(DGraph):
    def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

    def transpose(self):
        return self Graph
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


class DGraph:
    def __init__(self, n, edges):
        self._vertice = [{} for _ in range(n)]
        for e in edges:
            self._vertice[e[0]][e[1]] = e[2] if len(e) == 3 else 1

    def __len__(self):
        return len(self._vertice)

    def edges(self, n=None):
        srcs = range(len(self)) if n is None else [n]
        return sum([[(s, d, c) for d, c in self._vertice[s].items()] for s in srcs], [])

    def transpose(self):
        tedges = [(t, f, c) for f, t, c in self.edges()]
        return DGraph(len(self), tedges)

    def cost(self, src, dst):
        return self._vertice[src].get(dst, float('inf'))

class Graph(DGraph):
    def __init__(self, n, edges):
        nd_edges = edges + [(d, s, c) for s, d, c in edges]
        super().__init__(n, nd_edges)

    def transpose(self):
        return self



def solve(N: int, a: "List[int]", b: "List[int]", c: "List[int]"):
    edges = [(ca - 1, cb - 1, 0) for ca, cb in zip(a, b)]
    g = Graph(N, edges)
    s = [-1] * N

    c = deque(sorted(c, reverse=True))
    q = deque([0])
    while 0 < len(q):
        n = q.popleft()
        if s[n] != -1:
            continue
        s[n] = c.popleft()
        for _, d, _ in g.edges(n):
            q.append(d)

    ret = 0
    for b, e, _ in g.edges():
        ret += min(s[b], s[e])
    ret //= 2
    return '\n'.join([str(ret), ' '.join([str(n) for n in s])])

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int()] * (N-1)  # type: "List[int]" 
    b = [int()] * (N-1)  # type: "List[int]" 
    for i in range(N-1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    c = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    result = solve(N, a, b, c)
    if isinstance(result, Iterable) and not isinstance(result, str):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()
