Nw = gets.to_i W = gets.split.map(&:to_i) Nb = gets.to_i B = gets.split.map(&:to_i) A = [B, W] def f(h, i) x = A[i].select {|a| a < h}.max if x f(x, 1 - i) + 1 else 0 end end ans = [f(Float::INFINITY, 0), f(Float::INFINITY, 1)].max puts ans