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| arr_cp = Marshal.load(Marshal.dump(@arr)) sum = 0 a.each do |i| sum += arr_cp[i].inject(:+) @n.times { |j| arr_cp[j][i] = 0 } end max = [sum, max].max end max end end puts Yukicoder.new.run