import strutils proc nextString: string = result = "" while not endOfFile stdin: let nextChar = readChar stdin case nextChar of '\r': discard of "\n"[0], ' ': break else: add result, nextChar proc nextInt: int = parseInt nextString() let n, m = nextInt() var scoreList = newSeq[tuple[item1, item2, score: int]](m) ans = 0 proc calcScore(items: seq[int], x: int): int = for i in 0 ..< m: if x == scoreList[i].item2 and scoreList[i].item1 in items: result += scoreList[i].score proc dfs(items: seq[int], score: int) = if items.len == n: ans = max(ans, score) return for i in 0 ..< n: if i notin items: dfs(items & i, score + calcScore(items, i)) proc main: void = for i in 0 ..< m: scoreList[i] = (nextInt(), nextInt(), nextInt()) dfs(@[], 0) echo ans when isMainModule: main()