*D, = open(0)
n = int(D[0])
 
edge = [[] for _ in range(n+1)]
v_val = [0 for _ in range(n+1)]
 
dist = {}
 
for i in range(1, n):
    a, b = map(int, D[i].split())
    edge[a].append(b)
    edge[b].append(a)
c = list(map(int, D[n].split()))
 
c = sorted(c, reverse=True)
 
# 木の端を求める
def findLeaf(edge, start=1):
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    count = 0
    
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
    return leaf, count
 
 
# 木の直径を求める
def findD(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, count = findLeaf(edge, leaf)
    return count
 
# 木の中心を求める
def findCenter(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, d = findLeaf(edge, leaf)
    r = d//2
 
    visited = [False for _ in range(len(edge))]
    start = leaf
    next_v = edge[start]
    # print(next_v, leaf)
    visited[start] = True
    count = 0
 
    while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
    center = []
    for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
    return center
 
 
def calcCost(D, v_val):
    total = 0
    
    n = int(D[0])
    for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
    return total
 
 
max_val = 0
max_v = 0
l, _ = findLeaf(edge)
for center in [l]:
    order = []
    start = center
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    order.append(center)
 
    # Get order from center
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
            order.append(it)
        next_v = new_v
        
    # calculate cost
    v_val = [0 for _ in range(len(c)+1)]
    for i, it in enumerate(order):
        v_val[it] = c[i]
 
    val = calcCost(D, v_val)
    if val > max_val:
        max_val = val
        max_v = v_val
 
        
print(max_val)
print(' '.join(map(str, max_v[1:]))) *D, = open(0) *D D open(0) open 0 n = int(D[0]) n int(D[0]) int D[0] D 0 [] for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) [] edge = [[] for _ in range(n+1)] edge [[] for _ in range(n+1)] 0 for _ in range(n+1) for _ in range(n+1) _ range(n+1) range n+1 n 1 for _ in range(n+1) 0 v_val = [0 for _ in range(n+1)] v_val [0 for _ in range(n+1)] dist = {} dist {} for i in range(1, n):
    a, b = map(int, D[i].split())
    edge[a].append(b)
    edge[b].append(a) i range(1, n) range 1 n a, b = map(int, D[i].split()) a map(int, D[i].split()) map int D[i].split() [i].split [i] D i split b edge[a].append(b) [a].append [a] edge a append b edge[b].append(a) [b].append [b] edge b append a c = list(map(int, D[n].split())) c list(map(int, D[n].split())) list map(int, D[n].split()) map int D[n].split() [n].split [n] D n split c = sorted(c, reverse=True) c sorted(c, reverse=True) sorted c reverse=True reverse True def findLeaf(edge, start=1):
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    count = 0
    
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
    return leaf, count
 
 
# 木の直径を求める findLeaf False for _ in range(len(edge)) for _ in range(len(edge)) _ range(len(edge)) range len(edge) len edge for _ in range(len(edge)) False visited = [False for _ in range(len(edge))] visited [False for _ in range(len(edge))] next_v = edge[start] next_v edge[start] edge start visited[start] = True visited[start] visited start True count = 0 count 0 while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
     len(next_v) > 0 len(next_v) len next_v 0 new_v = [] new_v [] for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
         it next_v leaf = it leaf it for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
             v edge[it] edge it if not visited[v]:
                    new_v.append(v)
             not visited[v] visited[v] visited v new_v.append(v) new_v.append new_v append v visited[it] = True visited[it] visited it True next_v = new_v next_v new_v count += 1 count 1 return leaf, count return leaf, count leaf count edge edge start=1 start 1 def findD(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, count = findLeaf(edge, leaf)
    return count
 
# 木の中心を求める findD leaf, _ = findLeaf(edge, 1) leaf findLeaf(edge, 1) findLeaf edge 1 _ leaf, count = findLeaf(edge, leaf) leaf findLeaf(edge, leaf) findLeaf edge leaf count return count count edge edge def findCenter(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, d = findLeaf(edge, leaf)
    r = d//2
 
    visited = [False for _ in range(len(edge))]
    start = leaf
    next_v = edge[start]
    # print(next_v, leaf)
    visited[start] = True
    count = 0
 
    while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
    center = []
    for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
    return center
 
  findCenter leaf, _ = findLeaf(edge, 1) leaf findLeaf(edge, 1) findLeaf edge 1 _ leaf, d = findLeaf(edge, leaf) leaf findLeaf(edge, leaf) findLeaf edge leaf d r = d//2 r d//2 d 2 False for _ in range(len(edge)) for _ in range(len(edge)) _ range(len(edge)) range len(edge) len edge for _ in range(len(edge)) False visited = [False for _ in range(len(edge))] visited [False for _ in range(len(edge))] start = leaf start leaf next_v = edge[start] next_v edge[start] edge start visited[start] = True visited[start] visited start True count = 0 count 0 while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
     not r == count+1 r == count+1 r count+1 count 1 new_v = [] new_v [] for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
         it next_v leaf = it leaf it for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
             v edge[it] edge it if not visited[v]:
                    new_v.append(v)
             not visited[v] visited[v] visited v new_v.append(v) new_v.append new_v append v visited[it] = True visited[it] visited it True next_v = new_v next_v new_v count += 1 count 1 center = [] center [] for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
     it next_v if len(edge[it]) > 1:
            center.append(it)
     len(edge[it]) > 1 len(edge[it]) len edge[it] edge it 1 center.append(it) center.append center append it return center center edge edge def calcCost(D, v_val):
    total = 0
    
    n = int(D[0])
    for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
    return total
 
  calcCost total = 0 total 0 n = int(D[0]) n int(D[0]) int D[0] D 0 for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
     i range(1, n) range 1 n a, b = map(int, D[i].split()) a map(int, D[i].split()) map int D[i].split() [i].split [i] D i split b total += min(v_val[a], v_val[b]) total min(v_val[a], v_val[b]) min v_val[a] v_val a v_val[b] v_val b return total total D D v_val v_val max_val = 0 max_val 0 max_v = 0 max_v 0 l, _ = findLeaf(edge) l findLeaf(edge) findLeaf edge _ for center in [l]:
    order = []
    start = center
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    order.append(center)
 
    # Get order from center
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
            order.append(it)
        next_v = new_v
        
    # calculate cost
    v_val = [0 for _ in range(len(c)+1)]
    for i, it in enumerate(order):
        v_val[it] = c[i]
 
    val = calcCost(D, v_val)
    if val > max_val:
        max_val = val
        max_v = v_val
 
         center [l] l order = [] order [] start = center start center False for _ in range(len(edge)) for _ in range(len(edge)) _ range(len(edge)) range len(edge) len edge for _ in range(len(edge)) False visited = [False for _ in range(len(edge))] visited [False for _ in range(len(edge))] next_v = edge[start] next_v edge[start] edge start visited[start] = True visited[start] visited start True order.append(center) order.append order append center while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
            order.append(it)
        next_v = new_v
        
    # calculate cost
     len(next_v) > 0 len(next_v) len next_v 0 new_v = [] new_v [] for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
            order.append(it)
         it next_v leaf = it leaf it for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
             v edge[it] edge it if not visited[v]:
                    new_v.append(v)
             not visited[v] visited[v] visited v new_v.append(v) new_v.append new_v append v visited[it] = True visited[it] visited it True order.append(it) order.append order append it next_v = new_v next_v new_v 0 for _ in range(len(c)+1) for _ in range(len(c)+1) _ range(len(c)+1) range len(c)+1 len(c) len c 1 for _ in range(len(c)+1) 0 v_val = [0 for _ in range(len(c)+1)] v_val [0 for _ in range(len(c)+1)] for i, it in enumerate(order):
        v_val[it] = c[i]
 
     i it enumerate(order) enumerate order v_val[it] = c[i] v_val[it] v_val it c[i] c i val = calcCost(D, v_val) val calcCost(D, v_val) calcCost D v_val if val > max_val:
        max_val = val
        max_v = v_val
 
         val > max_val val max_val max_val = val max_val val max_v = v_val max_v v_val print(max_val) print max_val print(' '.join(map(str, max_v[1:]))) print ' '.join(map(str, max_v[1:])) ' '.join ' ' join map(str, max_v[1:]) map str max_v[1:] max_v 1: 1 val = calcCost(D, v_val) calcCost(D, v_val) val max_val = val val max_val max_v = v_val v_val max_v b = map(int, D[i].split()) map(int, D[i].split()) b c = sorted(c, reverse=True) sorted(c, reverse=True) c v_val = [0 for _ in range(len(c)+1)] [0 for _ in range(len(c)+1)] v_val max_val = 0 0 max_val _ = findLeaf(edge) findLeaf(edge) _ def findD(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, count = findLeaf(edge, leaf)
    return count
 
# 木の中心を求める def findD(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, count = findLeaf(edge, leaf)
    return count
 
# 木の中心を求める findD v_val = [0 for _ in range(n+1)] [0 for _ in range(n+1)] v_val visited = [False for _ in range(len(edge))] [False for _ in range(len(edge))] visited edge = [[] for _ in range(n+1)] [[] for _ in range(n+1)] edge max_v = 0 0 max_v start = center center start leaf = it it leaf next_v = edge[start] edge[start] next_v n = int(D[0]) int(D[0]) n c = list(map(int, D[n].split())) list(map(int, D[n].split())) c l, _ = findLeaf(edge) findLeaf(edge) l a, b = map(int, D[i].split()) map(int, D[i].split()) a def findCenter(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, d = findLeaf(edge, leaf)
    r = d//2
 
    visited = [False for _ in range(len(edge))]
    start = leaf
    next_v = edge[start]
    # print(next_v, leaf)
    visited[start] = True
    count = 0
 
    while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
    center = []
    for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
    return center
 
  def findCenter(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, d = findLeaf(edge, leaf)
    r = d//2
 
    visited = [False for _ in range(len(edge))]
    start = leaf
    next_v = edge[start]
    # print(next_v, leaf)
    visited[start] = True
    count = 0
 
    while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
    center = []
    for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
    return center
 
  findCenter dist = {} {} dist def findLeaf(edge, start=1):
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    count = 0
    
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
    return leaf, count
 
 
# 木の直径を求める def findLeaf(edge, start=1):
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    count = 0
    
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
    return leaf, count
 
 
# 木の直径を求める findLeaf def calcCost(D, v_val):
    total = 0
    
    n = int(D[0])
    for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
    return total
 
  def calcCost(D, v_val):
    total = 0
    
    n = int(D[0])
    for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
    return total
 
  calcCost order = [] [] order next_v = new_v new_v next_v new_v = [] [] new_v
*D, = open(0)
n = int(D[0])
 
edge = [[] for _ in range(n+1)]
v_val = [0 for _ in range(n+1)]
 
dist = {}
 
for i in range(1, n):
    a, b = map(int, D[i].split())
    edge[a].append(b)
    edge[b].append(a)
c = list(map(int, D[n].split()))
 
c = sorted(c, reverse=True)
 
# 木の端を求める
def findLeaf(edge, start=1):
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    count = 0
    
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
 
    return leaf, count
 
 
# 木の直径を求める
def findD(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, count = findLeaf(edge, leaf)
    return count
 
# 木の中心を求める
def findCenter(edge):
    leaf, _ = findLeaf(edge, 1)
    leaf, d = findLeaf(edge, leaf)
    r = d//2
 
    visited = [False for _ in range(len(edge))]
    start = leaf
    next_v = edge[start]
    # print(next_v, leaf)
    visited[start] = True
    count = 0
 
    while not r == count+1:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
        next_v = new_v
        count += 1
        
    center = []
    for it in next_v:
        if len(edge[it]) > 1:
            center.append(it)
    return center
 
 
def calcCost(D, v_val):
    total = 0
    
    n = int(D[0])
    for i in range(1, n):
        a, b = map(int, D[i].split())
        total += min(v_val[a], v_val[b])
        
    return total
 
 
max_val = 0
max_v = 0
l, _ = findLeaf(edge)
for center in [l]:
    order = []
    start = center
    visited = [False for _ in range(len(edge))]
    next_v = edge[start]
    visited[start] = True
    order.append(center)
 
    # Get order from center
    while len(next_v) > 0:
        new_v = []
        for it in next_v:
    #         print(it)
            leaf = it
            for v in edge[it]:
                if not visited[v]:
                    new_v.append(v)
            visited[it] = True
            order.append(it)
        next_v = new_v
        
    # calculate cost
    v_val = [0 for _ in range(len(c)+1)]
    for i, it in enumerate(order):
        v_val[it] = c[i]
 
    val = calcCost(D, v_val)
    if val > max_val:
        max_val = val
        max_v = v_val
 
        
print(max_val)
print(' '.join(map(str, max_v[1:])))