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("5"); WillReturn.Add("2 2 2 1 2"); //2 //1列目のブロックを3列目に移動し、 //5列目のブロックを4列目に移動すると、 //ピラミッド配置になるので、2個のブロックを移動するとピラミッド配置になる } else if (InputPattern == "Input2") { WillReturn.Add("3"); WillReturn.Add("3 2 3"); //4 //4つのレンガを捨て置き場に移動すると、ピラミッド配置になる } else if (InputPattern == "Input3") { WillReturn.Add("9"); WillReturn.Add("1 1 1 1 1 1 1 1 1"); //4 } else if (InputPattern == "Input4") { WillReturn.Add("3"); WillReturn.Add("1 2 1"); //0 //すでにピラミッド配置になっている } else if (InputPattern == "Input5") { WillReturn.Add("1"); WillReturn.Add("4"); //3 //最初が1列しかない場合でも3つ動かすことで3列のピラミッドができる } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); //レンガを捨てるのも、移動させるのも、同じコストなので //最大のピラミッドを作成するコストを求めればOK //最大のピラミッドの段数を求める int RengaCnt = AArr.Sum(); int PyramidHeight = (int)Math.Sqrt(RengaCnt); int KasanVal = 1; int CurrHeight = 1; int Answer = 0; for (int I = 0; I <= AArr.GetUpperBound(0); I++) { //Console.WriteLine("現在のピラミッドの高さ={0}", CurrHeight); if (CurrHeight < AArr[I]) { Answer += AArr[I] - CurrHeight; } if (CurrHeight > 0) { if (CurrHeight == PyramidHeight) { KasanVal = -1; } CurrHeight += KasanVal; } } Console.WriteLine(Answer); } }