Problem:
Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

4 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994

Solution in C# (.NET Core 3.1.201) (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = int.Parse(Console.ReadLine());

            //入力した値を数値変換する
            string str = Console.ReadLine();
            string[] strArray = new string[num];
            strArray = str.Split(' ');
            int[] intArray = Array.ConvertAll(strArray, int.Parse);

            // int型を格納するリストを宣言
            var intlist = new List<int>();

            //要素数-4
            int intLeaveEleNum = num - 4;

            int intResult = 0;

            //もし残った数がゼロなら
            if (intLeaveEleNum == 0)
            {
                //ソートする
                Array.Sort(intArray);

                //一番最後の配列-一番最初の配列
                intResult = intArray[num - 1] - intArray[0];


            }
            else
            {
                //for文
                for(int intBox1EleNum=0;intBox1EleNum< intLeaveEleNum+1; intBox1EleNum++)
                {
                    int intBox2Max = intLeaveEleNum - intBox1EleNum;
                    for(int intBox2EleNum=0; intBox2EleNum< intBox2Max+1; intBox2EleNum++)
                    {
                        int intBox3Max = intLeaveEleNum - intBox1EleNum - intBox2EleNum;
                        for(int intBox3EleNum=0;intBox3EleNum< intBox3Max+1;intBox3EleNum++)
                        {
                            int intBox4EleNum = intLeaveEleNum - intBox1EleNum - intBox2EleNum - intBox3EleNum;

                            int int1=intBox1EleNum+1;
                            int int2=intBox2EleNum+1;
                            int int3=intBox3EleNum+1;
                            int int4=intBox4EleNum+1;
                            
                            int[] intArrayResult = new int[4];

                            for(int i=0;i< int1;i++)
                            {
                                intArrayResult[0] = intArrayResult[0] + intArray[i];
                            }

                            for (int i = int1; i < int1 + int2; i++)
                            {
                                intArrayResult[1] = intArrayResult[1] = intArrayResult[1] + intArray[i];
                            }

                            for(int i= int1+ int2;i< int1 + int2+ int3;i++)
                            {
                                intArrayResult[2] = intArrayResult[2] = intArrayResult[2] + intArray[i];
                            }

                            for (int i = int1 + int2 + int3 ; i < int1 + int2 + int3+int4; i++)
                            {
                                intArrayResult[3] = intArrayResult[3] = intArrayResult[3] + intArray[i];
                            }

                            //ソートする
                            Array.Sort(intArrayResult);

                            //一番最後の配列-一番最初の配列
                            int intGap = intArrayResult[3] - intArrayResult[0];

                            
                            intlist.Add(intGap);

                        }
                    }
                }

            }

            intlist.Sort();

            int aaa = intlist[0];

            //出力
            Console.WriteLine(aaa);
        }
    }
}