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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    internal static class Program
    {
        public static void Main(string[] args)
        {
            // ARC100 D - Equal Cut
            var scanner = new StreamScanner(Console.OpenStandardInput());
            var n = scanner.Integer();
 
            var arr = new int[n];
            for (var i = 0; i < n; i++)
            {
                arr[i] = scanner.Integer();
            }

            var iM = n - 3;
            var jM = n - 2;
            var kM = n - 1;

            long iV = 0;
            long jV = arr[0];
            long kV = arr[1];
            long lV = arr.Skip(2).Sum();

            long bestScore = long.MaxValue;
            
            for (var i = 0; i < n - 3; i++)
            {
                iV += arr[i];
                jV -= arr[i];
                for (var j = i + 1; j < n - 2; j++)
                {
                    jV += arr[j];
                    kV -= arr[j];
                    for (var k = j + 1; k < n - 1; k++)
                    {
                        kV += arr[k];
                        lV -= arr[k];
                        var min = Math.Min(Math.Min(iV, jV), Math.Min(kV, lV));
                        var max = Math.Max(Math.Max(iV, jV), Math.Max(kV, lV));
                        var score = Math.Abs(max - min);
                        if (score < bestScore)
                        {
                            bestScore = score;
                        }
                    }
                }
            }
            Console.WriteLine(bestScore);
        }
    }
    
    // https://qiita.com/Camypaper/items/de6d576fe5513743a50e
    public class StreamScanner
    {   
        private readonly Stream stream;
        private readonly byte[] buf = new byte[1024];
        private int len, pos;
        private bool isEof = false;
        public bool IsEndOfStream => isEof;

        public StreamScanner(Stream stream) { this.stream = stream; }
        
        private byte Read()
        {
            if (isEof) throw new EndOfStreamException();
            if (pos >= len) {
                pos = 0;
                if ((len = stream.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[pos++];
        }
        
        public char Char() 
        {
            byte b = 0;
            do b = Read();
            while (b < 33 || 126 < b);
            return (char)b; 
        }
        
        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)Read())
                sb.Append(b);
            return sb.ToString();
        }
        
        public long Long()
        {
            long ret = 0; byte b = 0; var ng = false;
            do b = Read();
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-') { ng = true; b = Read(); }
            for (; true; b = Read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        
        public int Integer() { return (int)Long(); }
        public double Double() { return double.Parse(Scan(), CultureInfo.InvariantCulture); }
    }
}