$>.sync = true N = gets.to_i $res = Hash.new [] def f(a) while a.size > 1 new_a = [] a.each_slice(2){|x, y| if y.nil? new_a << x next end puts"? #{x} #{y}" z = gets.to_i new_a << z if z == x $res[x] += [y] else $res[y] += [x] end } a = new_a end a[0] end first = f([*1..N]) second = f($res[first]) puts "! #{second}"