fun readInt () = valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn) fun nList 0 = [0] | nList n = n :: nList (n - 1) fun insert x l = let val i = ref 0 val generated = ref [] in ( while !i <= List.length l do ( generated := (List.take (l, !i) @ (x :: (List.drop (l, !i)))) :: !generated; i := !i + 1 ); !generated ) end fun permutations nil = nil | permutations [x] = [[x]] | permutations (h::tl) = let val perm_rest = permutations tl val generated = ref [] in ( List.app (fn ll => List.app (fn lll => generated := lll :: !generated) (insert h ll) ) perm_rest; !generated ) end val () = let val n = readInt () val m = readInt () fun makeScoreTable () = let val scores = List.tabulate (m, fn _ => (readInt (), readInt (), readInt ())) val table = Array.tabulate (n, fn _ => Array.array (n, 0)) in ( List.app (fn (a, b, s) => Array.update (Array.sub (table, a), b, s)) scores; table ) end val scoreTable = makeScoreTable () val allCombinations = permutations (nList (n - 1)) fun findScore [] = 0 | findScore [_] = 0 | findScore (h::tl) = let val line = Array.sub (scoreTable, h) in (List.foldl (fn (x, acc) => Array.sub (line, x) + acc) 0 tl) + findScore tl end val ans = List.foldl (fn (l, acc) => Int.max (findScore l, acc)) 0 allCombinations in print (Int.toString ans ^ "\n") end