class Yukicoder def initialize @n, @m = gets.chomp.split.map(&:to_i) @arr = Array.new(@n) { Array.new(@n, 0) } @m.times do a, b, c = gets.chomp.split.map(&:to_i) @arr[a][b] = c end end def run # 最大値 max = 0 # 0〜@n-1で作る列の組み合わせを作成するO(n!) (0...@n).to_a.permutation(@n).each do |a| # # 9!でarrayを作成するのは厳しい # arr_cp = Marshal.load(Marshal.dump(@arr)) sum = 0 rest = (0...@n).to_a a.each do |i| rest.each do |j| sum += @arr[i][j] end rest -= [i] end max = [sum, max].max end max end end puts Yukicoder.new.run