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 static Input;

public class Prog
{
    private const int INF = 1000000007;
    private struct Pair
    {
        public long x, y;
        public Pair(long x, long y) { this.x = x; this.y = y; }
        public long Max() { return Math.Max(x, y); }
        public long Min() { return Math.Min(x, y); }
    }
    public static void Solve()
    {
        int N = NextInt();
        long[] A = LineLong();
        long[] B = new long[N + 1];
        for (int i = 1; i <= N; i++) B[i] += B[i - 1] + A[i - 1];
        long[] C = new long[N + 1];
        for (int i = 1; i <= N; i++) C[i] += C[i - 1] + A[N - i];
        //隙間はN-1個ある
        long ans = long.MaxValue;
        for (int mid = 1; mid < N - 2; mid++)
        {
            //なるべく均一にしたい
            //左側
            Pair[] lp = new Pair[2];
            int left_mid = Math.Max(LowerBound(B, B[mid + 1] / 2), 1);
            int right_mid = Math.Min(left_mid + 1, mid);

            lp[0] = new Pair(B[left_mid], B[mid + 1] - B[left_mid]);
            lp[1] = new Pair(B[right_mid], B[mid + 1] - B[right_mid]);

            Pair[] rp = new Pair[2];
            right_mid = Math.Max(LowerBound(C, C[N - mid - 1] / 2), 1);
            left_mid = Math.Min(right_mid + 1, N - mid - 2);

            rp[0] = new Pair(C[N - mid - 1] - C[right_mid], C[right_mid]);
            rp[1] = new Pair(C[N - mid - 1] - C[left_mid], C[left_mid]);

            for (int a = 0; a < 2; a++)
                for (int b = 0; b < 2; b++)
                {
                    ans =Math.Min(ans, Math.Max(lp[a].Max(), rp[b].Max())
                         - Math.Min(lp[a].Min(), rp[b].Min()));
                    //Console.WriteLine(lp[a].x + " " + lp[a].y + " "
                    //    + rp[b].x + " " + rp[b].y);
                }
        }
        Console.WriteLine(ans);

    }
    private static int LowerBound(long[] list, long n)
    {
        int a = 0, b = list.Count();
        if (list[b - 1] <= n) return b - 1;
        else if (list[a] > n) return -1;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] <= n) a = x;
            else b = x;
        }
        return a;
    }
    public static void Main()
    {
        var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
}
public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}