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++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100110],x[4];
int main()
{
	int n,i,j,k,l,maxx=-1,maxv=-1;
	cin>>n;
	for(i=1;i<=n;i++)
	{
	cin>>a[i];
	b[i]=b[i-1]+a[i];
	}
	b[i]=b[i-1];b[i+1]=b[i];b[i+2]=b[i];b[i+3]=b[i];b[i+4]=b[i];
	j=1;i=2;k=3;l=4;
		int l1=l,i1=1,j1=j,k1=k,ii,jj;
	while(l<=n+3)
	{
		maxx=-1;maxv=-1;
	    l1=l;i1=i;j1=j;k1=k;
		l++;
     	if(b[l]-b[k]*2+b[i]>b[k+1]*2-b[l]-b[i]) k++;
		if(b[k]-b[i]*2+b[j]>b[i+1]*2-b[k]-b[j]) i++;
		if(b[i]-b[j]*2>b[j+1]*2-b[i]) j++;
		x[0]=b[j]-b[0];
	    x[1]=b[i]-b[j];
	    x[2]=b[k]-b[i];
	    x[3]=b[l]-b[k];
	    for(ii=0;ii<4;ii++)
	    for(jj=ii+1;jj<4;jj++)
     	maxx=max(maxx,abs(x[jj]-x[ii]));
     	
		l1++;
		if(b[l1]-b[i1]*2>b[i1+1]*2-b[l1]) i1++;
     	if(b[l1]-b[k1]*2+b[i1]>b[k1+1]*2-b[l1]-b[i1]) k1++;
		if(b[i1]-b[j1]*2>b[j1+1]*2-b[i1]) j1++;
     	x[0]=b[j1]-b[0];
	    x[1]=b[i1]-b[j1];
	    x[2]=b[k1]-b[i1];
	    x[3]=b[l1]-b[k1];
	    for(ii=0;ii<4;ii++)
	    for(jj=ii+1;jj<4;jj++)
     	maxv=max(maxv,abs(x[jj]-x[ii]));
     	if(maxx>maxv)
     	{
     		i=i1;j=j1;k=k1;l=l1;
		 }
	}
	maxx=-1;maxv=-1;
	x[0]=b[j]-b[0];
	x[1]=b[i]-b[j];
	x[2]=b[k]-b[i];
	x[3]=b[l]-b[k];
	for(i=0;i<4;i++)
	{  
	for(j=i+1;j<4;j++)
	maxx=max(maxx,abs(x[j]-x[i]));}
cout<<maxx;
	return 0;
}