N = gets.to_i S = (1 << N) DP = Array.new(S,-1) DP[0] = 0 U = N.times.map{ gets.split.map(&:to_i) } def make_unit(s) if DP[s] < 0 N.times do |i| N.times do |j| if i > j && (s & 1 << i) > 0 && (s & 1 << j) > 0 ex = (1 << i) | (1 << j) like = make_unit(s ^ ex) + U[i][j] if like > DP[s] DP[s] = like end end end end end return DP[s] end make_unit(S-1) puts DP[S-1]