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: Accepted)

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <time.h>
#include <list>

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define reps(x,c) for(auto x:c)
#define all(x) begin(x), end(x)
#define pb(a) emplace_back(a) 
#define mp(a,b) make_pair((a),(b))
#define pl pair<ll,ll>
#define INF 2147483600
#define MOD 1000000007
#define fi first
#define se second
#define dbg(x) cout << #x"="<< (x) << endl
#define dbgv(x) cout<<#x<<endl;reps(y,x){cout<<y<<" ";}cout<<endl

ll N;
ll bit[1000010];

void bit_init(vector<ll> v){
	if(!v.empty()){
		N=v.size();
		for(ll x=1; x <= N; ++x) bit[x] = v[x];
		for(ll x=1; x < N; ++x) bit[x+(x&-x)] += bit[x];
	}
}
void bit_add(ll a, ll w){
		for(ll x=a; x <= N; x+=x&-x)bit[x]+=w;
}
ll bit_sum(ll a){
	ll ret=0;
	for(ll x=a;x>0;x-=x&-x) ret+=bit[x];
	return ret;
}


int main(){
	ll n;cin>>n;
	vector<ll> a(n+1);

	rep(i,n){
	  cin>>a[i+1];
	}
	bit_init(a);

	ll index1=1,index2=2,b,c,d,e,max_num,min_num,ans=1e10;
	FOR(i,2,n-1){
		b=bit_sum(i)-bit_sum(index1);
		c=bit_sum(index1);
		while(abs(bit_sum(i)-2*bit_sum(index1+1))<abs(bit_sum(i)-2*bit_sum(index1))){
			index1++;
			b=bit_sum(i)-bit_sum(index1);
			c=bit_sum(index1);
		}

		d=bit_sum(n)-bit_sum(index2);
		e=bit_sum(index2)-bit_sum(i);
		while(abs(bit_sum(n)-2*bit_sum(index2+1)+bit_sum(i))<abs(bit_sum(n)-2*bit_sum(index2)+bit_sum(i))){
			index2++;
			d=bit_sum(n)-bit_sum(index2);
			e=bit_sum(index2)-bit_sum(i);
		}
		max_num=max(b,max(c,max(d,e)));
	  min_num=min(b,min(c,min(d,e)));
	  ans=min(ans,max_num-min_num);
	}
  cout<<ans<<endl;
}