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