def f(a, b) _party = a.map{|v| v * MOD}.sort troop = b.map{|v|v/2} troop.size.times.map{ party = _party.dup troop.each{|elv| lv_cnt = party.shift + elv * MOD + 1 nth = party.bsearch_index{|v| v >= lv_cnt} || party.size party.insert(nth, lv_cnt) } troop.rotate! party.map{|v| v % MOD}.max }.min end N = gets.to_i A = gets.split.take(N).map(&:to_i) B = gets.split.take(N).map(&:to_i) MOD = 2000 p f(A, B)