from strutils import parseInt from sequtils import mapIt, newSeqWith from algorithm import binarySearch proc ask(x, y: int16): int16 = echo "? ", x, " ", y result = stdin.readLine.parseInt.int16 proc ans(x: int16) = echo "! ", x let N = stdin.readLine.parseInt var histories = newSeqWith(9, newSeq[int16]()) histories[0] = (1..N).mapIt(it.int16) for h in 0..<8: if histories[h].len <= 1: break for i in countup(1, histories[h].high, 2): histories[h + 1].add(ask(histories[h][i - 1], histories[h][i])) if histories[h].len mod 2 == 1: histories[h + 1].add(histories[h][^1]) var results = newSeq[int16]() max: int16 for h in countdown(8, 0): if histories[h].len > 1: let i = histories[h].binarySearch(max) if i mod 2 == 0: if i + 1 < histories[h].len: results.add(histories[h][i + 1]) else: if i - 1 >= 0: results.add(histories[h][i - 1]) elif histories[h].len == 1: max = histories[h][0] continue else: continue var next = newSeq[int16]() while results.len > 1: next = @[] for i in countup(1, results.high, 2): next.add(ask(results[i - 1], results[i])) if results.len mod 2 == 1: next.add(results[^1]) results = next ans(results[0])