class Party attr_reader :size def initialize(level_list) @level_list = level_list.clone @size = @level_list.size end def clone return self.class.new(@level_list) end def level_each_with_start_pos(start_pos) @size.times do |i| pos = (start_pos + i) % @size yield @level_list[pos] end end end class OwnParty < Party def initialize(level_list) super @count_list = [0] * @level_list.size end def clone clone_op = super clone_op.set_count_list(@count_list) return clone_op end def set_count_list(count_list) @count_list = count_list.clone end def max_count return @count_list.max end def next_index count_max_over = max_count + 1 min = (0...@size).min_by do |i| @level_list[i] * count_max_over - @count_list[i] end return min end def level_up(i, exp) @count_list[i] += 1 @level_list[i] += exp end end gets own = OwnParty.new(gets.split.map(&:to_i)) mon = Party.new(gets.split.map(&:to_i)) list = (0..mon.size).map do |i| own_c = own.clone mon.level_each_with_start_pos(i) do |level| own_c.level_up(own_c.next_index, level / 2) end own_c.max_count end puts list.min