結果

問題 No.78 クジ付きアイスバー
ユーザー DialBird
提出日時 2017-02-23 13:53:18
言語 Ruby
(3.4.1)
結果
AC  
実行時間 90 ms / 5,000 ms
コード長 727 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 8,192 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2025-01-02 18:25:12
合計ジャッジ時間 4,820 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Yukicoder
  def initialize
    @n, @k = gets.chomp.split.map(&:to_i)
    @ice_box = gets.chomp.split('').map(&:to_i)
  end

  def run
    atari_stock = 0
    necessary_buy = Array.new(@n+1, 0)
    @n.times do |i|
      if atari_stock > 0
        atari_stock -= 1
        necessary_buy[i + 1] = necessary_buy[i]
      else
        necessary_buy[i + 1] = necessary_buy[i] + 1
      end
      atari_stock += @ice_box[i]
    end

    if @k <= @n
      return necessary_buy[@k]
    else
      interval_buy = [necessary_buy[@n] - atari_stock, 0].max
      last_buy = [necessary_buy[@k % @n] - atari_stock, 0].max
      return necessary_buy[@n] + (@k / @n - 1) * interval_buy + last_buy
    end
  end
end

puts Yukicoder.new.run
0