n, m = gets.chomp.split.map(&:to_i) @scores = Array.new(n) { Array.new(n,0) } @dp = {} m.times do it_1, it_2, s = gets.chomp.split.map(&:to_i) @scores[it_1][it_2] = s end def solve(used, rest) return 0 if rest.empty? @dp[used.sort!] ||= rest.map do |i| sum = 0 used.each { |j| sum += @scores[i][j] } sum += solve(used + [i], rest - [i]) end.max end puts solve([], [*0...n])