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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int N;
        public long[] A, S;
        public int[] D1, D2;
        // 中心の切る位置をiとした場合の、切る位置（左側）、切る位置（右側）
        // 切る位置は、[begin, c] [c+1, last]

        public void Run()
        {
            // Input
            N = Input.ReadInt();
            A = Input.ReadLongArray();

            S = new long[N];
            for (int i = 0; i < N; i++)
                S[i] = (i == 0 ? 0 : S[i - 1]) + A[i];

            // しゃくとり法
            D1 = new int[N];
            {
                int i = 0;
                int c = 0;
                while (i < N)
                {
                    if (IsBestL(i, c))
                    {
                        D1[i] = c;
                        i++;
                    }
                    else
                    {
                        c++;
                    }
                }
            }

            D2 = new int[N];
            {
                int i = N - 1;
                int c = N - 1;
                while (i >= 0)
                {
                    if (IsBestR(i, c))
                    {
                        D2[i] = c;
                        i--;
                    }
                    else
                    {
                        c--;
                    }
                }
            }


            long ret = long.MaxValue;
            for (int i = 0; i < N; i++)
            {
                int c1 = D1[i];
                int c2 = i;
                int c3 = D2[i];
                int c4 = N - 1;
                long p1 = S[c1]; ;
                long p2 = S[c2] - S[c1];
                long p3 = S[c3] - S[c2];
                long p4 = S[c4] - S[c3];

                long mn = Math.Min(p1, Math.Min(p2, Math.Min(p3, p4)));
                long mx = Math.Max(p1, Math.Max(p2, Math.Max(p3, p4)));
                ret = Math.Min(ret, mx - mn);
            }

            Console.WriteLine(ret);

        }

        public bool IsBestL(int i, int c)
        {
            if (c == i) return true;
            long s = S[i];
            long sc = S[c];
            long sn = S[c + 1];
            long current = Math.Abs(s - 2 * sc);
            long next = Math.Abs(s - 2 * sn);
            return current <= next;
        }

        public bool IsBestR(int i, int c)
        {
            if (c == i) return true;
            long s = S[N - 1] - S[i];
            long sc = S[N - 1] - S[c];
            long sn = S[N - 1] - S[c - 1];
            long current = Math.Abs(s - 2 * sc);
            long next = Math.Abs(s - 2 * sn);
            return current <= next;
        }

    }

    // libs ----------


    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}