n = gets.to_i score = Array.new(n,0) @free = [] n.times{|i| j=0;gets.chomp!.chars{|c| case c when 'o';score[i]-=1 when '-';@free<<[i,j].sort! end j+=1 } } @free.uniq! def dfs(i,score) if @free.size==i # チーム番号を保持してソート,team-Kを線形探索 i=-1; return score.map{|e|i+=1;[e,i]}.sort!.index{|e|e[1]==0}+1 else e = @free[i] s = score.clone ; s[e[0]]-=1 ra = dfs(i+1,s) s = score.clone ; s[e[1]]-=1 rb = dfs(i+1,s) return [ra,rb].min end end p dfs(0,score)