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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		Sum = new long[N+1];
		for(int i=0;i<N;i++) Sum[i+1] = Sum[i] + A[i];
		
		long min = (long)1e18;
		for(int i=2;i<=N-2;i++){
			int l1 = 1, r1 = i;
			int l = 1, r = i;
			if(i == 2){
				l1 = 1; r1 = 1;
			} else if(Sum[l] > Sum[i] - Sum[l]){
				l1 = 1; r1 = 1;
			} else {
				while(r - l > 1){
					int c = (l + r) / 2;
					if(Sum[c] <= Sum[i] - Sum[c]){
						l = c;
					} else {
						r = c;
					}
				}
				l1 = l; r1 = r;
			}
			
			int l2 = i + 1, r2 = N;
			
			l = i + 1; r = N;
			if(Sum[l] - Sum[i]> Sum[N] - Sum[l]){
				l2 = i + 1; r2 = i + 1;
			} else {
				while(r - l > 1){
					int c = (l + r) / 2;
					if(Sum[c] - Sum[i]<= Sum[N] - Sum[c]){
						l = c;
					} else {
						r = c;
					}
				}
				l2 = l; r2 = r;
			}
			
			
//Console.WriteLine("i:{0}, {1} {2} {3} {4}",i,l1,r1,l2,r2);			
			
			
			long[] v = new long[4];
			long Inf = (long) 1e18;
			long ma = 0, mi = 0;
			v[0] = Sum[l1]; v[1] = Sum[i] - Sum[l1]; v[2] = Sum[l2] - Sum[i]; v[3] = Sum[N] - Sum[l2];
			ma = -Inf; mi = Inf;
			for(int j=0;j<4;j++){
				ma = Math.Max(ma, v[j]);
				mi = Math.Min(mi, v[j]);
			}
			min = Math.Min(min, ma - mi);
			
			v[0] = Sum[r1]; v[1] = Sum[i] - Sum[r1]; v[2] = Sum[l2] - Sum[i]; v[3] = Sum[N] - Sum[l2];
			ma = -Inf; mi = Inf;
			for(int j=0;j<4;j++){
				ma = Math.Max(ma, v[j]);
				mi = Math.Min(mi, v[j]);
			}
			min = Math.Min(min, ma - mi);
			
			v[0] = Sum[l1]; v[1] = Sum[i] - Sum[l1]; v[2] = Sum[r2] - Sum[i]; v[3] = Sum[N] - Sum[r2];
			ma = -Inf; mi = Inf;
			for(int j=0;j<4;j++){
				ma = Math.Max(ma, v[j]);
				mi = Math.Min(mi, v[j]);
			}
			min = Math.Min(min, ma - mi);
			
			v[0] = Sum[r1]; v[1] = Sum[i] - Sum[r1]; v[2] = Sum[r2] - Sum[i]; v[3] = Sum[N] - Sum[r2];
			ma = -Inf; mi = Inf;
			for(int j=0;j<4;j++){
				ma = Math.Max(ma, v[j]);
				mi = Math.Min(mi, v[j]);
			}
			min = Math.Min(min, ma - mi);
			
		}
		Console.WriteLine(min);
	}
	
	long[] Sum;
	
	
	int N;
	long[] A;
	public Sol(){
		N = ri();
		A = rla();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}