class Yukicoder def initialize n = gets.chomp.to_i a = gets.chomp.split(' ').map(&:to_i) min_move_count = Float::INFINITY 1.upto(100) do |num| array = pyramid_array(num) move_count = 0 from_count = 0 to_count = 0 break if a.inject(:+) < array.inject(:+) a.each_with_index do |num, index| count = num - (array[index] || 0) if count < 0 from_count += count else to_count += count end end move_count = [from_count, to_count].min + [from_count, to_count].max - [from_count, to_count].min min_move_count = [min_move_count, move_count].min end puts min_move_count end def pyramid_array(num) [1] if num == 1 [*(1..num-1)] + [num] + [*(1..num-1)].reverse end end Yukicoder.new