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: Wrong Answer)

using System;
using System.Diagnostics;
using System.Collections.Generic;

class Myon

{
    public Myon() { }
    public static int Main()
    {
        cin = new Scanner();
        new Myon().calc();
        return 0;
    }
    static Scanner cin;


    int N;
    long[] A;
    long[] dp;
    void calc()
    {
        N = cin.nextInt();
        A = cin.ArrayLong(N);
        dp = new long[N + 1];
        for (int i = 0; i < N; i++)
        {
            dp[i + 1] = dp[i] + A[i];
        }

        long ans = long.MaxValue;

        int d1 = 1;
        int d2 = 3;
        for (int c = 2; c < N - 2; c++)
        {
            if (c == d2) d2++;

            long A = sum(0, d1);
            long B = sum(d1, c);
            long C = sum(c, d2);
            long D = sum(d2, N);
            while(A < B && d1 < c)
            {
                d1++;
                A = sum(0, d1);
                B = sum(d1, c);
            }
            while (C < D && d2 < N)
            {
                d2++;
                C = sum(c, d2);
                D = sum(d2, N);
            }

            ans = Math.Min(ans, maxmin4(sum(0, d1), sum(d1, c), sum(c, d2), sum(d2, N)));
            ans = Math.Min(ans, maxmin4(sum(0, d1), sum(d1, c), sum(c, d2-1), sum(d2-1, N)));
            ans = Math.Min(ans, maxmin4(sum(0, d1-1), sum(d1-1, c), sum(c, d2), sum(d2, N)));
            ans = Math.Min(ans, maxmin4(sum(0, d1-1), sum(d1-1, c), sum(c, d2-1), sum(d2-1, N)));

        }
        Console.WriteLine(ans);


    }

    long maxmin4(long a, long b, long c, long d)
    {
        return Math.Max(Math.Max(a, b), Math.Max(c, d)) - Math.Min(Math.Min(a, b), Math.Min(c, d));
    }

    long sum(int a, int b)
    {
        return dp[b] - dp[a];
    }
    
}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}