require 'byebug' require 'pp' n = gets[0].to_i input = STDIN.read.split("\n") Criteria = Struct.new(:item1, :item2, :score) crt = [] input.each do |item| tmp = item.split.map!(&:to_i) crt << Criteria.new(tmp[0], tmp[1], tmp[2]) end ans = 0 (0..(n - 1)).to_a.permutation do |i| tmp = 0 crt.each do |crt| if i.index(crt.item1) < i.index(crt.item2) tmp += crt.score end ans = [ans, tmp].max end end #byebug #pp crt puts ans