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("4"); WillReturn.Add("1 2 3 4"); } else if (InputPattern == "Input2") { WillReturn.Add("4"); WillReturn.Add("5 4 4 9"); } else if (InputPattern == "Input3") { WillReturn.Add("7"); WillReturn.Add("1 2 9 10 1 1 4"); } else if (InputPattern == "Input4") { WillReturn.Add("1"); WillReturn.Add("100"); } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); int[] VArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); int UB = VArr.GetUpperBound(0); //添字が皿の合計、値が美味しさ合計の最大値 int[] SumVArr = new int[UB + 1]; for (int I = 0; I <= UB; I++) { if (I == 0 || I == 1) { SumVArr[I] = VArr[I]; continue; } //配列の添字の上限までの範囲での、最大値を返す Func DeriveHaniMaxFunc = pJyougenInd => { int WillReturn = SumVArr[0]; for (int J = 0; J <= pJyougenInd; J++) { if (WillReturn < SumVArr[J]) WillReturn = SumVArr[J]; } return WillReturn; }; //その皿を取るノードでの美味しさ合計の最大値 int Node1SumV = DeriveHaniMaxFunc(I - 2) + VArr[I]; //その皿を取らないノードでの美味しさ合計の最大値 int Node2SumV = DeriveHaniMaxFunc(I - 1); SumVArr[I] = Math.Max(Node1SumV, Node2SumV); } Console.WriteLine(SumVArr.Max()); } }