class BinaryIndexTree def initialize(size, init: 0) @values = Array.new(size, init) @size = size end # @param idx [Integer] # @param x [Numeric] def add(idx, x) raise 'Out of range reference' if @size <= idx idx += 1 while idx <= @size @values[idx - 1] += x idx += idx & -idx end end def sum(l, r) _sum(r) - _sum(l) end private def _sum(idx) res = 0 while idx > 0 res += @values[idx - 1] idx -= idx & -idx end res end end N = gets.to_i A = gets.split.map(&:to_i) B = gets.split.map(&:to_i) rank = Hash.new A.each.with_index(1) do |a, i| rank[a] = i end bit = BinaryIndexTree.new(N + 1) ans = [] B.each do |b| br = rank[b] if bit.sum(br, N + 1) == 0 ans << b end bit.add(br, 1) end puts ans.sort