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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define llong long long
#define fr(i,l,r) for(i=(l);i<(r);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000//10^12

int main(void)
{
	//変数の宣言
	int n;
	static llong a[200010];
	static llong ruiseki[200010];
	llong int p,q,r,s;
	int left,right;
	int m;
	
	//よく使う変数
	int i,j,k,l;
	int flag=0;
	llong int ans=0;
	int count=0;
	int temp,temp1,temp2;
	llong int max,min;
	int len;
	int sum=0;
	//データの読み込み
	
	scanf("%d",&n);
	
	for(i=0;i<n;i++){
		scanf("%lld",&a[i]);
	}
	
	
	
//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理
	
	for(i=0;i<n;i++){
		if(i==0){
			ruiseki[0]=a[0];
		}else{
			ruiseki[i]=ruiseki[i-1]+a[i];
		}
	}
	
	ans=INF;
	for(i=1;i<n-2;i++){
		m=i;
		
		flag=0;
		left=0;right=m;
		while(flag==0){
			if(right-left<=1){
				if(r==m){
					p=ruiseki[left];
					q=ruiseki[m]-ruiseki[left];
					flag=1;
				}else if(llabs(ruiseki[m]-2*ruiseki[left])<llabs(ruiseki[m]-2*ruiseki[right])){
					p=ruiseki[left];
					q=ruiseki[m]-ruiseki[left];
					flag=1;
				}else{
					p=ruiseki[right];
					q=ruiseki[m]-ruiseki[right];
					flag=1;
				}
			}else{
				temp=(left+right)/2;
				if(ruiseki[m]<2*ruiseki[temp]){
					right=temp;
				}else{
					left=temp;
				}
			}
		}
		
		flag=0;
		left=m+1;right=n-1;
		while(flag==0){
			if(right-left<=1){
				if(r==n-1){
					r=ruiseki[left]-ruiseki[m];
					s=ruiseki[n-1]-ruiseki[left];
					flag=1;
				}else if(llabs(ruiseki[n-1]+ruiseki[m]-2*ruiseki[left])<llabs(ruiseki[n-1]+ruiseki[m]-2*ruiseki[right])){
					r=ruiseki[left]-ruiseki[m];
					s=ruiseki[n-1]-ruiseki[left];
					flag=1;
				}else{
					r=ruiseki[right]-ruiseki[m];
					s=ruiseki[n-1]-ruiseki[right];
					flag=1;
				}
			}else{
				temp=(left+right)/2;
				if(ruiseki[n-1]+ruiseki[m]<2*ruiseki[temp]){
					right=temp;
				}else{
					left=temp;
				}
			}
		}
		
		min=min(min(p,q),min(r,s));
		max=max(max(p,q),max(r,s));
		
		ans=min(ans,max-min);
	}
//	printf("計算部分終了\n");
	//出力
	
	printf("%lld",ans);
	
//	printf("結果の出力終了\n");
	
	return 0;
	
}