using System; using System.Collections.Generic; using System.Linq; //No.385 カップ麺生活 class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("100"); WillReturn.Add("2"); WillReturn.Add("47 50"); //5 } else if (InputPattern == "Input2") { WillReturn.Add("500"); WillReturn.Add("5"); WillReturn.Add("111 222 333 444 555"); //8 } else if (InputPattern == "Input3") { WillReturn.Add("2447"); WillReturn.Add("3"); WillReturn.Add("9 6 10"); //79842 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); int M = int.Parse(InputList[0]); Eratosthenes(M); int[] CArr = InputList[2].Split(' ').Select(X => int.Parse(X)).ToArray(); Array.Sort(CArr); //昇順にソートしておく //最大購入数[残金]なDP表 var DPArr = new Nullable[M + 1]; DPArr[M] = 0; foreach (int EachCost in CArr) { for (int I = M; 0 <= I; I--) { if (DPArr[I].HasValue == false) continue; int NewInd = I - EachCost; if (NewInd < 0) break; int NewVal = DPArr[I].Value + 1; if (DPArr[NewInd].HasValue == false || DPArr[NewInd].Value < NewVal) { DPArr[NewInd] = NewVal; } } } int Answer = 0; //残金が素数の購入数を集計 for (int I = 0; I <= M; I++) { if (DPArr[I].HasValue == false) continue; if (SosuuArr.Contains(I) == false) continue; Answer += DPArr[I].Value; } //最大の購入数を加算 Answer += DPArr.Max(X => X ?? 0); Console.WriteLine(Answer); } static int[] SosuuArr; //エラトステネスの篩 static void Eratosthenes(int pJyougen) { var CheckArr = new System.Collections.BitArray(pJyougen + 1); for (int I = 2; I <= CheckArr.Count - 1; I++) { CheckArr[I] = true; } for (int I = 2; I <= CheckArr.Count - 1; I++) { if (I != 2 && I % 2 == 0) continue; if (CheckArr[I]) { for (int J = I * 2; J <= CheckArr.Count - 1; J += I) { CheckArr[J] = false; } } } var SosuuList = new List(); for (int I = 2; I <= CheckArr.Count - 1; I++) if (CheckArr[I]) SosuuList.Add(I); SosuuArr = SosuuList.ToArray(); } }