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"); WillReturn.Add("3"); WillReturn.Add("1 2 3"); WillReturn.Add("3 2 1"); } else if (InputPattern == "Input2") { WillReturn.Add("9"); WillReturn.Add("2"); WillReturn.Add("8 2"); WillReturn.Add("9 7"); } else if (InputPattern == "Input3") { WillReturn.Add("9"); WillReturn.Add("2"); WillReturn.Add("8 2"); WillReturn.Add("9 5"); } else if (InputPattern == "Input4") { WillReturn.Add("20"); WillReturn.Add("5"); WillReturn.Add("10 7 9 5 7"); WillReturn.Add("5 8 2 8 1"); } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } struct JyoutaiDef { internal int CurrP; internal int CSum; internal int VSum; internal int[] CurrVArr; internal string Path; } static void Main() { List InputList = GetInputList(); int T = int.Parse(InputList[0]); int[] CArr = InputList[2].Split(' ').Select(X => int.Parse(X)).ToArray(); int[] VArr = InputList[3].Split(' ').Select(X => int.Parse(X)).ToArray(); int UB = CArr.GetUpperBound(0); var stk = new Stack(); JyoutaiDef WillPush; WillPush.CurrP = 0; WillPush.CSum = 0; WillPush.VSum = 0; WillPush.CurrVArr = VArr; WillPush.Path = ""; stk.Push(WillPush); int AnswerVSum = 0; string AnswerPath = ""; while (stk.Count > 0) { JyoutaiDef Popped = stk.Pop(); //Console.WriteLine("満足度合計={0}", Popped.VSum); //Console.WriteLine(Popped.Path); //解の更新 if (AnswerVSum < Popped.VSum) { AnswerVSum = Popped.VSum; AnswerPath = Popped.Path; } for (int I = Popped.CurrP; I <= UB; I++) { //対象アトラクションの満足度が0ならContinue; int CurrV = Popped.CurrVArr[I]; if (CurrV == 0) continue; //Push処理 WillPush.CurrP = I; WillPush.CSum = Popped.CSum + CArr[I]; if (WillPush.CSum > T) continue; WillPush.VSum = Popped.VSum + CurrV; WillPush.CurrVArr = (int[])Popped.CurrVArr.Clone(); WillPush.CurrVArr[I] /= 2; //満足度の更新 WillPush.Path = Popped.Path + Environment.NewLine + string.Format("アトラクション{0}(満足度{1})", I, CurrV); //下限値枝切り int RestSum = WillPush.VSum; for (int J = WillPush.CurrP; J <= UB; J++) { int wkV = WillPush.CurrVArr[J]; while (true) { RestSum += wkV; if ((wkV /= 2) == 0) break; } } if (RestSum < AnswerVSum) continue; stk.Push(WillPush); } } //Console.WriteLine("最大満足度={0}", AnswerVSum); //Console.WriteLine(AnswerPath); Console.WriteLine(AnswerVSum); } }