def solve nums = gets.chomp.split.map(&:to_i) if nums[1] <= nums[2] # 参加者がいない場合 puts 0 else price_by_person = calc_by_person(nums) total_price = price_by_person * (nums[1] - nums[2]) puts total_price end end def calc_by_person(nums) d_price = nums[3] return d_price if nums[0] <= 1 prices_of_drops = [d_price] # 渡す個数に応じて計算 (2..nums[0]).each do |i| x = prices_of_drops.last if (i % 10).zero? e = nums[4] # E price if e <= x prices_of_drops << [x - e, 0].max else prices_of_drops << x end else prices_of_drops << x end end prices_of_drops.sum end solve