using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input6"; 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 if (InputPattern == "Input5") { //最悪計算量 WillReturn.Add("50 2000000000"); WillReturn.Add(new string('0', 50)); } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static int K; static char[] SArr; static void Main() { //var sw = System.Diagnostics.Stopwatch.StartNew(); List InputList = GetInputList(); K = int.Parse(InputList[0].Split(' ').Last()); SArr = InputList[1].ToCharArray(); Solve(); //Console.WriteLine("経過時間={0}", sw.Elapsed); } static void Solve() { int HonsuuSum = 0; int KounyuuCnt = 0; int AtariHonsuu = 0; for (int HakoCnt = 1; ; HakoCnt++) { foreach (char EachChar in SArr) { if (AtariHonsuu == 0) KounyuuCnt++; else AtariHonsuu--; if (EachChar == '1') AtariHonsuu++; if (EachChar == '2') AtariHonsuu += 2; if (++HonsuuSum == K) { Console.WriteLine(KounyuuCnt); return; } } //Console.WriteLine("{0}箱目の結果。購入数={1}。当たり本数={2}", // HakoCnt, KounyuuCnt, AtariHonsuu); } } }