n = gets.not_nil!.to_i a = Array.new(n) { gets.not_nil!.to_i } b = Array.new(n) { gets.not_nil!.to_i } ans = Int32::MAX n.times do |i| t = 0 # Create a min-heap using an array sorted by first element ta = a.map { |val| {val, 0} }.sort_by { |pair| pair[0] } n.times do |j| # Get the smallest element (first in sorted array) p = ta.shift # Update the value and count new_val = p[0] + b[(i + j) % n] // 2 new_count = p[1] + 1 # Keep track of maximum count t = Math.max(t, new_count) # Insert back into sorted position using bsearch insert_index = ta.bsearch_index { |x| x[0] >= new_val } || ta.size ta.insert(insert_index, {new_val, new_count}) end ans = Math.min(ans, t) end puts ans