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: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using static System.Console;
using System.Text;
using System.IO;
using static System.Math;
using System.Numerics;

namespace AtCoder
{
    class Program
    {
        static public long[] Sarray() { return ReadLine().Split().Select(long.Parse).ToArray(); }
        static public List<long> Slist() { return ReadLine().Split().Select(long.Parse).ToList(); }
        static long Mod = (long)1e9 + 7;
        static void Main(string[] args)
        {
            SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
            Solve();
            Out.Flush();
            Read();
        }
        static void Solve()
        {
            var N = Sarray()[0];
            var Ai = Sarray();
            rui = new long[N + 1];
            for (var i = 0; i < N; ++i)
                rui[i + 1] = Ai[i] + rui[i];
            var min = long.MaxValue;
            for (var l = 2; l < N - 1; ++l)
                min = Min(min, Calc1(l,N));
            WriteLine(min);
        }
        static long[] rui;
        //[0,l)と[l,N)にわける
        static long Calc1(long l,long N)
        {
            var ll = binarysearch(0, l);
            var rr = binarysearch(l, N);

            var m1 = rui[ll + 1];
            var m2 = rui[l] - rui[ll + 1];
            var m4 = rui[ll + 2];
            var m3 = rui[l] - rui[ll + 2];
            var l1 = m3;
            var l2 = m4;
            if (Abs(m1 - m2) < Abs(m3 - m4))
            {
                l1 = m1;
                l2 = m2;
            }

            var m5 = rui[rr + 1] - rui[l];
            var m6 = rui[N] - rui[rr + 1];
            var m7 = rui[rr + 2] - rui[l];
            var m8 = rui[N] - rui[rr + 2];
            var r1 = m7;
            var r2 = m8;
            if (Abs(m5 - m6) < Abs(m7 - m8))
            {
                r1 = m5;
                r2 = m6;
            }
            var max = Max(Max(r1, r2), Max(l1, l2));
            var min = Min(Min(r1, r2), Min(l1, l2));

            return Abs(max - min);
        }
        //[l,r)間で<sum/2となるところを探す
        static long binarysearch(long l, long r)
        {
            var s = l;
            var J = (rui[r] - rui[s]) / 2;
            while (1 < r - l)
            {
                var mid = l + (r - l) / 2;
                if (rui[mid + 1] - rui[s] < J)
                    l = mid;
                else
                    r = mid;
            }
            return l;
        }
    }
}