nw = gets.to_i ws = gets.strip.split.map(&:to_i) nb = gets.to_i bs = gets.strip.split.map(&:to_i) ws.sort! ws.reverse! bs.sort! bs.reverse! def search(ws, bs) h = 0 while !ws.empty? || !ws.empty? w = ws.shift h += 1 bs = bs.select { |b| b < w } if bs.empty? break else b = bs.shift h += 1 ws = ws.select { |w| w < b } end end return h end puts [search(ws, bs), search(bs, ws)].max()