def g;gets.split.map(&:to_i)end n,m=g S=Hash[(1..m).map{*a,b=g;[a,b]}] H={} def dfs(done,left) H[done.sort!]||=left.map{|i|done.inject(0){|t,j|t+(S[[i,j]]||0)}+dfs(done+[i],left-[i])}.max||0 end p dfs([],[*0...n])