Language: C++

package main

import (
	"bufio"
	"fmt"
	"io"
	"math"
	"os"
	"strconv"
	"strings"

)


func main() {
	solve(os.Stdin, os.Stdout)
}

type ReaderEx struct {
	baseScanner *bufio.Scanner
	words       []string
	nextWordIdx func() int
	initWordIdx func(int)
}

func NewScannerEx(stdin io.Reader) *ReaderEx {
	var this ReaderEx
	var idx int
	var wordsCnt int
	this.baseScanner = bufio.NewScanner(stdin)
	this.baseScanner.Buffer(make([]byte, 0), 1E+11)
	this.nextWordIdx = func() int {
		cur := idx
		idx = (idx + 1) % wordsCnt
		return cur
	}
	this.initWordIdx = func(wc int) {
		idx = 0
		wordsCnt = wc
	}
	return &this
}

func (me *ReaderEx) Scan(doSplit bool) (isScanned bool) {
	isScanned = me.baseScanner.Scan()
	if doSplit {
		me.words = strings.Fields(me.baseScanner.Text())
	} else {
		me.words = []string{me.baseScanner.Text()}
	}
	me.initWordIdx(len(me.words))
	return
}

func (me *ReaderEx) nextBytes() []byte {
	return []byte(me.words[me.nextWordIdx()])
}
func (me *ReaderEx) nextString() string {
	return me.words[me.nextWordIdx()]
}

type StringArray []string

func (me *ReaderEx) nextStringArray() StringArray {
	return me.words[me.nextWordIdx():]
}

func (me StringArray) toStrings() []string {
	return me
}

func (me StringArray) toInts() []int {
	strValues := me.toStrings()
	values := make([]int, len(strValues))
	for i, v := range strValues {
		n, err := strconv.Atoi(v)
		if err != nil {
			panic(err)
		}
		values[i] = n
	}
	return values
}
func (me *ReaderEx) nextInt() int {
	n, err := strconv.Atoi(me.words[me.nextWordIdx()])

	defer func() {
		if err != nil {
			panic(fmt.Sprintf("getInt(%d)", n))
		}
	}()
	return n
}

func pow(x, y, mod int) (ans int) {

	ans = 1
	for bit := 1 << uint(math.Floor(math.Log2(float64(y)))); ; bit >>= 1 {
		if y&bit != 0 {
			ans *= x
			if mod > 0 {
				ans %= mod
			}
		}
		if bit <= 1 {
			return
		}
		ans *= ans
		if mod > 0 {
			ans %= mod
		}
	}
}

type factType struct {
	prime int
	count int
}

func fact(n int) (facts []*factType) {

	for i := 2; i*i <= n; i++ {
		if n%i == 0 {
			fa := factType{prime: i}
			for n%fa.prime == 0 {
				n /= fa.prime
				fa.count++
			}
			facts = append(facts, &fa)
		}
	}
	if n > 1 {
		facts = append(facts, &factType{prime: n, count: 1})
	}
	return facts
}
func dec2bin(n int) (ret []bool) {
	for bit := 1 << uint(math.Floor(math.Log2(float64(n)))); ; bit >>= 1 {
		ret = append(ret, n&bit != 0)

		if bit <= 1 {
			return
		}
	}

}

func abs(v int) int {
	if v < 0 {
		v = -v
	}
	return v
}

func b2i(b bool) (i int) {
	if b {
		i = 1
	} else {
		i = 0
	}
	return
}

//RBNodeDir is
type RBNodeDir int

const (

	//RBNodeLeft is
	RBNodeLeft RBNodeDir = iota

	//RBNodeRight is
	RBNodeRight

	//RBNodeHere is
	RBNodeHere
)

//RBNode is
type RBNode struct {
	isRed    bool
	Index    int
	parent   *RBNode
	children [2]*RBNode
}

//RBTree is
type RBTree struct {
	Node *RBNode
	less func(i int, j int) bool
}

//RBCursor is
type RBCursor RBTree

//NewTree is
func NewTree(less func(i int, j int) bool) *RBTree {
	tree := &RBTree{less: less}
	return tree
}

//Cursor is
func (cur *RBTree) Cursor() *RBCursor {
	wrk := RBCursor(*cur)
	return &wrk
}

func (cur *RBTree) root() *RBTree {

	if cur.Node != nil {
		for ; cur.Node.parent != nil; cur.Node = cur.Node.parent {

		}
	}
	return cur
}

//Move is
func (cur *RBCursor) Move(dir RBNodeDir) *RBCursor {
	rev := dir ^ 1
	if next := cur.Node.children[dir]; next != nil {
		for next.children[rev] != nil {
			next = next.children[rev]
		}
		cur.Node = next
	} else {
		find := false
		for cur.Node.parent != nil {
			now := cur.Node
			cur.Node = cur.Node.parent
			if cur.Node.children[rev] == now {
				find = true
				break
			}
		}
		if !find {
			cur.Node = nil
		}
	}
	return cur
}

//Find is
func (cur *RBTree) Find(Index int) (*RBTree, RBNodeDir) {
	dir := RBNodeLeft
	if cur.Node == nil {
		return cur, dir
	}
	cur.root()
	for {
		if cur.less(Index, cur.Node.Index) {
			dir = RBNodeLeft
			next := cur.Node.children[dir]
			if next == nil {
				break
			}
			cur.Node = next

		} else if cur.less(cur.Node.Index, Index) {
			dir = RBNodeRight
			next := cur.Node.children[dir]
			if next == nil {
				break
			}
			cur.Node = next
		} else {
			dir = RBNodeHere
			break
		}
	}
	return cur, dir
}

//Add is
func (cur *RBTree) Add(Index int) *RBTree {
	newNode := &RBNode{Index: Index, isRed: true}
	cur.root()
	if cur.Node != nil {
		pnode, dir := cur.Find(newNode.Index)
		if dir != RBNodeHere {
			newNode.parent = pnode.Node
			newNode.parent.children[dir] = newNode
			newNode.opt()
		} else {
			pnode.Node.Index = newNode.Index
			newNode = pnode.Node
		}
	}
	cur.Node = newNode
	return cur
}

func (Node *RBNode) flip(dir RBNodeDir) {
	curGranPa := Node.parent
	newParent := Node.children[dir]
	if curGranPa != nil {
		curGranPa.children[Node.dir()] = newParent
	}
	newParent.parent, Node.parent = Node.parent, newParent

	Node.children[dir] = newParent.children[dir^1]
	if Node.children[dir] != nil {
		Node.children[dir].parent = Node
	}
	newParent.children[dir^1] = Node

}
func (Node *RBNode) dir() RBNodeDir {
	dir := RBNodeLeft
	if Node.parent.children[dir] != Node {
		dir = RBNodeRight
	}
	return dir
}
func (Node *RBNode) opt() {
	for Node != nil && Node.isRed {
		parent := Node.parent
		if parent == nil {
			break
		} else if !parent.isRed {
			break
		} else if parent.parent == nil {
			parent.isRed = false
		} else {
			grandparent := parent.parent
			parentsibling := grandparent.children[parent.dir()^1]
			if parentsibling != nil && parentsibling.isRed {
				grandparent.isRed = true
				parent.isRed = false
				parentsibling.isRed = false
				Node = grandparent
			} else {
				dir := parent.dir()

				if parent.children[dir] != Node {
					parent.flip(dir ^ 1)
				}

				grandparent.flip(dir)
				grandparent.parent.isRed = false
				grandparent.isRed = true
				break
			}
		}
	}
}

//End is
func (cur *RBTree) End(dir RBNodeDir) *RBCursor {
	cur.root()
	if cur.Node != nil {
		for ; cur.Node.children[dir] != nil; cur.Node = cur.Node.children[dir] {

		}
	}
	return cur.Cursor()
}

func (Node *RBNode) cut() {
	if Node.parent != nil {
		Node.parent.children[Node.dir()] = nil
	}
}

func (Node *RBNode) hasDir() RBNodeDir {
	dir := RBNodeLeft
	if Node.children[dir] == nil {
		dir = RBNodeRight
		if Node.children[dir] == nil {
			dir = RBNodeHere
		}
	}
	return dir
}

//Delete is
func (cur *RBTree) Delete(Index int) (ret bool) {
	wcur, dir := cur.Find(Index)
	if dir != RBNodeHere {
		return
	}
	delNode := wcur.Node
	ret = true

	dir = delNode.hasDir()

	if delNode.children[RBNodeLeft] != nil &&
		delNode.children[RBNodeRight] != nil {
		var wrk *RBCursor
		if dir != RBNodeHere {
			wrk = wcur.Cursor().Move(dir)
			delNode.Index = wrk.Node.Index
			delNode = wrk.Node
			dir = delNode.hasDir()
		}
	}

	if dir == RBNodeHere {
		if delNode.isRed {
			delNode.cut()
			cur.Node = delNode.parent
			return
		}
	} else {
		wrk := delNode.children[dir]
		delNode.Index = wrk.Index
		wrk.cut()
		return
	}

	Node := delNode
	for {
		parent := Node.parent
		if parent == nil {
			break
		}

		dir := Node.dir()
		dirOther := dir ^ 1
		sibling := parent.children[dirOther]

		if sibling.isRed {
			//sibling is Red
			parent.flip(dirOther)
			sibling.isRed = false
			parent.isRed = true
			sibling = parent.children[dirOther]
		}
		//sibling is Black

		nephew := sibling.children[dirOther]
		if nephew == nil || !nephew.isRed {
			//far nephew is Black
			nephew = sibling.children[dir]
			if nephew == nil || !nephew.isRed {
				//near nephew is Black
				sibling.isRed = true
				if parent.isRed {
					parent.isRed = false
					break
				} else {
					Node = parent
					continue
				}
			}
			//near nephew is Red and far nephew is Black
			sibling.flip(dir)
			sibling, nephew = nephew, sibling
			sibling.isRed = false
			nephew.isRed = true
		}
		//sibling is Black && far nephew is Red

		parent.flip(dirOther)
		sibling.isRed = parent.isRed
		parent.isRed = false
		nephew.isRed = false
		break
	}
	delNode.cut()
	cur.Node = delNode.parent
	return
}

type line struct {
	to, from int
	cost     int
}

type lineSort []line

func (a lineSort) Len() int           { return len(a) }
func (a lineSort) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a lineSort) Less(i, j int) bool { return a[i].cost < a[j].cost }

func solve(stdin io.Reader, stdout io.Writer) {

	r := NewScannerEx(stdin)
	r.Scan(true)
	h, _ := r.nextInt(), r.nextInt()

	c := make([][]line, 10)
	for i := range c {
		c[i] = make([]line, 0, 10)
	}
	for i := range c {
		r.Scan(true)

		for j, v := range r.nextStringArray().toInts() {
			if i != j {
				c[j] = append(c[j], line{from: i, to: j, cost: v})
			}
		}
	}

	list := make([]*line, 0, 10*10)

	tree := NewTree(func(i, j int) bool {
		left, right := list[i], list[j]
		switch {
		case left.cost != right.cost:
			return left.cost < right.cost
		case left.from != right.from:
			return left.from < right.from
		default:
			return left.to < right.to
		}
	})

	treeAdd := func(lp *line) {
		i := len(list)
		list = append(list, lp)
		tree.Add(i)
	}

	treeAdd(&line{from: 1, to: 1, cost: 0})
	gall := make([]*line, 10)
	gallcnt := 10

	for {
		l := tree.End(RBNodeLeft)
		lt := list[l.Node.Index]
		tree.Delete(l.Node.Index)
		if gall[lt.from] != nil {
			continue
		}

		gall[lt.from] = lt
		gallcnt--
		if gallcnt <= 0 {
			break
		}

		for i := range c[lt.from] {
			next := &c[lt.from][i]
			if next.from == lt.to {
				continue
			}
			next.cost += lt.cost
			treeAdd(next)
		}
	}

	cnt := map[int]int{}
	for i := 0; i < h; i++ {
		r.Scan(true)
		w := r.nextStringArray().toInts()
		for _, v := range w {
			if v > 0 {
				cnt[v]++
			}
		}
	}

	ans := 0
	for i, v := range cnt {
		ans += v * gall[i].cost
	}
	fmt.Fprintln(stdout, ans)
}


Compiler output:
a.cc:1:1: error: 'package' does not name a type
    1 | package main
      | ^~~~~~~
a.cc:19:1: error: 'type' does not name a type; did you mean 'typeof'?
   19 | type ReaderEx struct {
      | ^~~~
      | typeof
a.cc:26:1: error: 'func' does not name a type
   26 | func NewScannerEx(stdin io.Reader) *ReaderEx {
      | ^~~~
a.cc:44:6: error: expected constructor, destructor, or type conversion before '(' token
   44 | func (me *ReaderEx) Scan(doSplit bool) (isScanned bool) {
      |      ^
a.cc:55:6: error: expected constructor, destructor, or type conversion before '(' token
   55 | func (me *ReaderEx) nextBytes() []byte {
      |      ^
a.cc:58:6: error: expected constructor, destructor, or type conversion before '(' token
   58 | func (me *ReaderEx) nextString() string {
      |      ^
a.cc:62:1: error: 'type' does not name a type; did you mean 'typeof'?
   62 | type StringArray []string
      | ^~~~
      | typeof
a.cc:68:6: error: expected constructor, destructor, or type conversion before '(' token
   68 | func (me StringArray) toStrings() []string {
      |      ^
a.cc:72:6: error: expected constructor, destructor, or type conversion before '(' token
   72 | func (me StringArray) toInts() []int {
      |      ^
a.cc:84:6: error: expected constructor, destructor, or type conversion before '(' token
   84 | func (me *ReaderEx) nextInt() int {
      |      ^
a.cc:95:1: error: 'func' does not name a type
   95 | func pow(x, y, mod int) (ans int) {
      | ^~~~
a.cc:115:1: error: 'type' does not name a type; did you mean 'typeof'?
  115 | type factType struct {
      | ^~~~
      | typeof
a.cc:120:1: error: 'func' does not name a type
  120 | func fact(n int) (facts []*factType) {
      | ^~~~
a.cc:137:1: error: 'func' does not name a type
  137 | func dec2bin(n int) (ret []bool) {
      | ^~~~
a.cc:148:1: error: 'func' does not name a type
  148 | func abs(v int) int {
      | ^~~~
a.cc:155:1: error: 'func' does not name a type
  155 | func b2i(b bool) (i int) {
      | ^~~~
a.cc:165:1: error: 'type' does not name a type; did you mean 'typeof'?
  165 | type RBNodeDir int
      | ^~~~
      | typeof
a.cc:188:1: error: 'type' does not name a type; did you mean 'typeof'?
  188 | type RBTree struct {
      | ^~~~
      | typeof
a.cc:194:1: error: 'type' does not name a type; did you mean 'typeof'?
  194 | type RBCursor RBTree
      | ^~~~
      | typeof
a.cc:203:6: error: expected constructor, destructor, or type conversion before '(' token
  203 | func (cur *RBTree) Cursor() *RBCursor {
      |      ^
a.cc:208:6: error: expected constructor, destructor, or type conversion before '(' token
  208 | func (cur *RBTree) root() *RBTree {
      |      ^
a.cc:219:6: error: expected constructor, destructor, or type conversion before '(' token
  219 | func (cur *RBCursor) Move(dir RBNodeDir) *RBCursor {
      |      ^
a.cc:244:6: error: expected constructor, destructor, or type conversion before '(' token
  244 | func (cur *RBTree) Find(Index int) (*RBTree, RBNodeDir) {
      |      ^
a.cc:275:6: error: expected constructor, destructor, or type conversion before '(' token
  275 | func (cur *RBTree) Add(Index int) *RBTree {
      |      ^
a.cc:293:6: error: expected constructor, destructor, or type conversion before '(' token
  293 | func (Node *RBNode) flip(dir RBNodeDir) {
      |      ^
a.cc:308:6: error: expected constructor, destructor, or type conversion before '(' token
  308 | func (Node *RBNode) dir() RBNodeDir {
      |      ^
a.cc:315:6: error: expected constructor, destructor, or type conversion before '(' token
  315 | func (Node *RBNode) opt() {
      |      ^
a.cc:349:6: error: expected constructor, destructor, or type conversion before '(' token
  349 | func (cur *RBTree) End(dir RBNodeDir) *RBCursor {
      |      ^
a.cc:359:6: error: expected constructor, destructor, or type conversion before '(' token
  359 | func (Node *RBNode) cut() {
      |      ^
a.cc:365:6: error: expected constructor, destructor, or type conversion before '(' token
  365 | func (Node *RBNode) hasDir() RBNodeDir {
      |      ^
a.cc:377:6: error: expected constructor, destructor, or type conversion before '(' token
  377 | func (cur *RBTree) Delete(Index int) (ret bool) {
      |      ^
a.cc:465:1: error: 'type' does not name a type; did you mean 'typeof'?
  465 | type line struct {
      | ^~~~
      | typeof
a.cc:470:1: error: 'type' does not name a type; did you mean 'typeof'?
  470 | type lineSort []line
      | ^~~~
      | typeof
a.cc:473:6: error: expected constructor, destructor, or type conversion before '(' token
  473 | func (a lineSort) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
      |      ^
a.cc:474:6: error: expected constructor, destructor, or type conversion before '(' token
  474 | func (a lineSort) Less(i, j int) bool { return a[i].cost < a[j].cost }
      |      ^
a.cc:476:1: error: 'func' does not name a type
  476 | func solve(stdin io.Reader, stdout io.Writer) {
      | ^~~~