# Read n n = gets.not_nil!.to_i # Read array a (space-separated) a = gets.not_nil!.split.map(&.to_i) # Read array b (space-separated) b = gets.not_nil!.split.map(&.to_i) ans = Int32::MAX n.times do |i| t = 0 # Use a min-heap simulation with a sorted array # Store pairs as [value, count] and keep sorted by value ta = a.map { |val| {val, 0} }.sort_by { |pair| pair[0] } n.times do |j| # Get the smallest element smallest = ta.shift value, count = smallest # Update the value (use proper integer division that truncates toward zero) addition = b[(i + j) % n] new_value = value + (addition > 0 ? addition // 2 : (addition - 1) // 2 + 1) new_count = count + 1 # Track maximum count t = Math.max(t, new_count) # Find insertion position using binary search insert_index = ta.bsearch_index { |x| x[0] >= new_value } || ta.size ta.insert(insert_index, {new_value, new_count}) end ans = Math.min(ans, t) end puts ans