n = gets.not_nil!.to_i a = [] of {Int32, Int32} n.times do t = gets.not_nil!.to_i a << {t, 0} end # Sort the initial array to act as a min-heap a.sort_by! { |x| x[0] } b = Array(Int32).new(n) n.times do |i| b[i] = gets.not_nil!.to_i end ans = 10**9 n.times do |i| t = 0 # Create a deep copy of the array for simulation ta = a.map { |x| {x[0], x[1]} } n.times do |j| # Get the smallest element (like popping from min-heap) min_index = 0 ta.each_with_index do |element, idx| if element[0] < ta[min_index][0] min_index = idx end end p = ta.delete_at(min_index) # Update the element p = {p[0] + b[(i + j) % n] // 2, p[1] + 1} t = Math.max(t, p[1]) ta << p end ans = Math.min(ans, t) end puts ans