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 <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ctime>
#include <cassert>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <iostream>
#define REP(i,a,b)for (int i=a;i<=b;++i)
#define PER(i,a,b)for (int i=a;i>=b;--i)
#define RVC(i,S)for (int i=0;i<S.size();++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

typedef long long LL;
typedef pair<int,int> pii;
typedef vector<int> VI;

int n;
LL ai[200005];

inline LL S(int l,int r){
	return ai[r]-ai[l-1];
}

int main(){
	scanf("%d",&n);
	REP(i,1,n){
		scanf("%lld",&ai[i]);
		ai[i]+=ai[i-1];
	}
	LL avg=ai[n]/4;
	int pl=0,pr=0;
	REP(i,1,n-1)if(ai[i]<=avg&&ai[i+1]>=avg){
		pl=i;
		break;
	}
	PER(i,n-1,1)if(ai[n]-ai[i]<=avg&&ai[n]-ai[i-1]>=avg){
		pr=i;
		break;
	}
	assert(pl&&pr);
	LL ans=1ll<<60;
	REP(i,pl-10,pl+10)REP(j,pr-10,pr+10){
		if(i<1||i>n||j<0||j>n||i+2>=j)continue;
		LL t=S(i+1,j-1)/2;
		int l=i+1,r=j-1,res=i+1;
		while(l<=r){
			int mid=(l+r)>>1;
			if(S(i+1,mid)<=t){
				res=mid;
				l=mid+1;
			}else r=mid-1;
		}
		REP(k,res-10,res+10)if(i<k&&k+1<j){
			LL tmp[4]={S(1,i),S(i+1,k),S(k+1,j-1),S(j,n)};
			sort(tmp,tmp+4);
			ans=min(ans,tmp[3]-tmp[0]);
		}
	}
	printf("%lld\n",ans);
	return 0;
}