import strutils, sequtils, tables proc main() = let n = stdin.readLine.strip.parseInt a = stdin.readLine.strip.split.map(parseInt) var right = initTable[int, int]() for i in 0.. 0 and right[stack[^1]] < i: discard stack.pop if stack.len == 0 or stack[^1] < a[i]: stack.add(a[i]) b[i] = stack[^1] echo b.map(proc(it: int): string = $it).join(" ") main()