def scan; gets.chomp!.split.map(&:to_i); end def flip(turn) turn == :a_san ? :b_san : :a_san end @memo={} def dfs(c, turn = :a_san) c.delete(0) return flip(turn) if c.empty? return @memo[[c,turn]] if @memo[[c,turn]] c.each_index{|i| c[i] -= 1 return @memo[[c,turn]]=turn if dfs(c.clone, flip(turn)) == turn if turn == :b_san && 0 < c[i] c[i] -= 1 return @memo[[c,turn]]=turn if dfs(c.clone, flip(turn)) == turn c[i] += 1 end c[i] += 1 } return @memo[[c,turn]]=flip(turn) end gets c = scan # End Of Line abort if gets puts dfs(c) == :a_san ? 'A' : 'B'