class Yukicoder def initialize n, k = gets.chomp.split.map(&:to_i) s = gets.chomp list = Array.new(n, 0) buy_count = 0 atari_cost = 0 s.chars.each_with_index do |ch, index| if atari_cost > 0 atari_cost -= 1 else buy_count += 1 end list[index] = buy_count if ch == '1' atari_cost += 1 elsif ch == '2' atari_cost += 2 end end if n >= k puts list[k-1] else x = k / n - 1 if k % n == 0 y = [buy_count, atari_cost].map{|i| i * (x+1)} if y[1] > 0 puts [y[0] - y[1] + 1, 1].max else puts [y[0] - y[1], 1].max end else y = [buy_count, atari_cost].map{|i| i * x} puts [buy_count + x * (buy_count - atari_cost) + [list[k%n-1] - atari_cost, 0].max, buy_count].max end end end end Yukicoder.new