using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("1 2 3 4"); //6 //2 4 //お寿司の取り方は、 //「1個目と3個目」のお寿司を取る、 //「1個目と4個目」のお寿司を取る、 //または「2個目と4個目」のお寿司を取る方法があるが、 //2個目、4個目のお寿司を取ることで、最大の美味しさが得られる。 } else if (InputPattern == "Input2") { WillReturn.Add("4"); WillReturn.Add("5 4 4 9"); //14 //1 4 //1個目のお寿司と4個目のお寿司を取ることで //最大の美味しさの合計が得られる。 } else if (InputPattern == "Input3") { WillReturn.Add("7"); WillReturn.Add("1 2 9 10 1 1 4"); //16 //2 4 7 //2,10,4と取れば最大の16が得られる。 //10の後の6番目の1をあえてスルーしないといけない。 } else if (InputPattern == "Input4") { WillReturn.Add("1"); WillReturn.Add("100"); //100 //1 //1皿しかない場合もあります。 } else if (InputPattern == "Input5") { WillReturn.Add("275"); WillReturn.Add("40 84 19 1 55 8 8 67 54 96 21 82 83 33 74 15 11 72 13 75 28 11 42 81 32 11 47 29 47 61 60 35 31 13 75 38 90 99 26 90 39 63 96 13 58 84 67 82 89 78 78 39 20 99 100 18 5 36 69 38 29 82 89 35 57 18 86 40 30 51 36 47 45 43 40 55 73 37 20 86 29 25 49 76 66 18 71 54 58 87 2 62 69 97 71 39 88 72 17 86 68 48 26 40 89 8 53 49 51 18 61 53 45 67 49 85 52 67 71 99 26 91 34 69 35 34 58 24 64 55 72 51 69 82 29 65 78 28 43 19 74 79 50 49 62 1 75 41 50 20 38 17 35 13 18 26 69 36 16 58 37 54 50 27 94 32 62 94 62 41 84 79 35 48 13 58 96 44 31 44 88 76 77 57 65 23 40 64 49 32 2 42 2 8 90 100 28 16 91 52 57 68 64 96 45 35 52 80 47 72 66 61 69 86 23 45 35 44 73 45 71 14 26 19 5 10 1 15 37 98 18 8 2 38 82 58 52 3 100 34 32 29 96 46 14 29 88 36 59 64 3 74 51 5 66 23 68 5 65 71 45 3 93 66 69 70 81 56 14 75 16 82 5 82 38"); //7946 //2 5 8 10 13 15 18 20 22 24 27 29 31 33 35 38 40 43 45 47 49 51 53 55 57 59 61 63 65 67 70 72 74 76 78 80 83 85 87 90 92 94 96 98 100 102 105 107 109 111 114 116 118 120 122 124 127 129 131 133 135 137 139 141 143 145 147 149 151 153 155 157 160 162 165 168 170 172 174 177 179 181 183 185 188 190 192 195 197 199 202 204 206 208 210 212 214 216 219 221 223 226 228 230 232 235 237 239 241 243 245 247 250 252 255 257 259 261 263 265 267 270 272 274 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } struct JyoutaiDef { internal int CurrP; internal int SumVal; internal List SelectSushiList; } static void Main() { List InputList = GetInputList(); int[] VArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); int UB = VArr.GetUpperBound(0); var stk = new Stack(); JyoutaiDef WillPush; WillPush.CurrP = 0; WillPush.SumVal = 0; WillPush.SelectSushiList = new List(); stk.Push(WillPush); int AnswerSum = 0; var AnswerSelectSushiList = new List(); //メモ化探索用 var MemoDict = new Dictionary(); while (stk.Count > 0) { JyoutaiDef Popped = stk.Pop(); if (AnswerSum < Popped.SumVal) { AnswerSum = Popped.SumVal; AnswerSelectSushiList = Popped.SelectSushiList; } for (int I = Popped.CurrP; I <= UB && I <= Popped.CurrP + 1; I++) { WillPush.CurrP = I + 2; WillPush.SumVal = Popped.SumVal + VArr[I]; WillPush.SelectSushiList = new List(Popped.SelectSushiList) { I + 1 }; if (MemoDict.ContainsKey(WillPush.CurrP)) { if (MemoDict[WillPush.CurrP] >= WillPush.SumVal) continue; } MemoDict[WillPush.CurrP] = WillPush.SumVal; stk.Push(WillPush); } } Console.WriteLine(AnswerSum); IEnumerable wkEnum = AnswerSelectSushiList.Select(X => X.ToString()); Console.WriteLine(wkEnum.Aggregate((A, B) => A + " " + B)); } }