from scipy.sparse import*
n,*t=map(int,open(0).read().split())
c=sorted(t[-n:])
f=c[:]
for i in csgraph.depth_first_order(csr_matrix((f[1:],(t[:-n:2],t[1:-n:2])),[n+1]*2),1,0,0):n-=1;f[i-1]=c[n]
print(sum(c[:-1]),*f) from scipy.sparse import* n,*t=map(int,open(0).read().split()) n map(int,open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split *t t c=sorted(t[-n:]) c sorted(t[-n:]) sorted t[-n:] t -n: -n n f=c[:] f c[:] c : for i in csgraph.depth_first_order(csr_matrix((f[1:],(t[:-n:2],t[1:-n:2])),[n+1]*2),1,0,0):n-=1;f[i-1]=c[n] i csgraph.depth_first_order(csr_matrix((f[1:],(t[:-n:2],t[1:-n:2])),[n+1]*2),1,0,0) csgraph.depth_first_order csgraph depth_first_order csr_matrix((f[1:],(t[:-n:2],t[1:-n:2])),[n+1]*2) csr_matrix (f[1:],(t[:-n:2],t[1:-n:2])) f[1:] f 1: 1 (t[:-n:2],t[1:-n:2]) t[:-n:2] t :-n:2 -n n 2 t[1:-n:2] t 1:-n:2 1 -n n 2 [n+1]*2 [n+1] n+1 n 1 2 1 0 0 print(sum(c[:-1]),*f) print sum(c[:-1]) sum c[:-1] c :-1 -1 *f f n,*t=map(int,open(0).read().split()) map(int,open(0).read().split()) n f=c[:] c[:] f c=sorted(t[-n:]) sorted(t[-n:]) c
from scipy.sparse import*
n,*t=map(int,open(0).read().split())
c=sorted(t[-n:])
f=c[:]
for i in csgraph.depth_first_order(csr_matrix((f[1:],(t[:-n:2],t[1:-n:2])),[n+1]*2),1,0,0):n-=1;f[i-1]=c[n]
print(sum(c[:-1]),*f)