using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input5"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("5 4"); WillReturn.Add("01200"); //2 } else if (InputPattern == "Input2") { WillReturn.Add("2 8"); WillReturn.Add("10"); //4 } else if (InputPattern == "Input3") { WillReturn.Add("10 100"); WillReturn.Add("2222222222"); //1 } else if (InputPattern == "Input4") { WillReturn.Add("2 5"); WillReturn.Add("01"); //3 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static int K; static char[] SArr; static void Main() { List InputList = GetInputList(); K = int.Parse(InputList[0].Split(' ').Last()); SArr = InputList[1].ToCharArray(); Console.WriteLine(Solve()); } static int Solve() { int HonsuuSum = 0; int KounyuuCnt = 0; int AtariHonsuu = 0; //1箱分の処理を行う Action HitohakoSyori = () => { foreach (char EachChar in SArr) { if (AtariHonsuu == 0) KounyuuCnt++; else AtariHonsuu--; if (EachChar == '1') AtariHonsuu++; if (EachChar == '2') AtariHonsuu += 2; if (++HonsuuSum == K) { return; } } }; //1箱目の処理を行う HitohakoSyori(); //1箱目で指定本数に達した場合 if (HonsuuSum == K) { return KounyuuCnt; } //持ち越した当たり本数を使っての //次の箱での購入数 int SecondBoxKounyuuCnt = KounyuuCnt - AtariHonsuu; if (SecondBoxKounyuuCnt <= 0) return KounyuuCnt; //指定本数までの全箱数 int TotalBoxCnt = K / SArr.Length; if (K % SArr.Length > 0) TotalBoxCnt++; //1箱目と最終箱以外の箱数 int RestBoxCnt = TotalBoxCnt - 2; //1箱目と最終箱以外の箱を一気に処理 if (RestBoxCnt > 0) { HonsuuSum += RestBoxCnt * SArr.Length; KounyuuCnt += RestBoxCnt * SecondBoxKounyuuCnt; } //最終箱の処理を行う HitohakoSyori(); return KounyuuCnt; } }