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.Linq;
using System.Text;
using System.Diagnostics;

namespace AtCoder
{
    class Program
    {
        private static string Read() { return Console.ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }

        static Tuple<long, long> Search(int N, long[] S)
        {
            int i = 0;
            int j = N - 2;
            long sum = S[N - 1];

            if (S[i] >= sum - S[i])
            {
                long a = S[i];
                long b = sum - S[i];
                return new Tuple<long, long>(Math.Min(a, b), Math.Max(a, b));
            }

            if (S[j] <= sum - S[j])
            {
                long a = S[j];
                long b = sum - S[j];
                return new Tuple<long, long>(Math.Min(a, b), Math.Max(a, b));
            }

            while (j - i >= 2)
            {
                int m = (i + j) / 2;
                if (S[m] < sum - S[m]) { i = m; } else { j = m; }
            }

            long a1 = S[i];
            long b1 = sum - S[i];
            long dif1 = Math.Max(a1, b1) - Math.Min(a1, b1);

            long a2 = S[i + 1];
            long b2 = sum - S[i + 1];
            long dif2 = Math.Max(a2, b2) - Math.Min(a2, b2);

            return (dif1 <= dif2) ? new Tuple<long, long>(Math.Min(a1, b1), Math.Max(a1, b1))
                                  : new Tuple<long, long>(Math.Min(a2, b2), Math.Max(a2, b2));
        }
        static long Solve(int N, int[] A)
        {
            long[] a = new long[N];
            long[] b = new long[N];
            a[0] = A[0];
            b[0] = A[N - 1];
            for (int i = 1; i < N; ++i)
            {
                a[i] = a[i - 1] + A[i];
                b[i] = b[i - 1] + A[N - i - 1];
            }
            long min = long.MaxValue;
            for (int i = 2; N - i >= 2; ++i)
            {
                int j = N - i;
                var t1 = Search(i, a);
                var t2 = Search(j, b);
                min = Math.Min(Math.Max(t1.Item2, t2.Item2) - Math.Min(t1.Item1, t2.Item1), min);
            }
            return min;
        }
        static void Main(string[] args)
        {
            int N = ReadInt();
            int[] A = ReadInts();
            Console.WriteLine(Solve(N, A));
        }
    }
}