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++ (Clang 10.0.0) (judge verdict: Accepted)

//g++ -std=c++14 test.cpp -o test.out
 
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map> 
#include <unordered_map> 
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
 
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N;
LL A[200000];

LL ans = INF;

void solve(){
	vector<LL> accum(N,A[0]);
	for(int i=1;i<N;i++) accum[i] = accum[i-1] + A[i];

	//切れ込み3箇所のうち、真ん中を決めうち
	//インデックスi番目の直後に入れることを想定
	for(int middle=1;middle<=N-3;middle++){
		auto search = [&](int left_index,int right_index){
			LL left = left_index;
			LL right = right_index-1;
			while(left+1<right){
				LL center = (left+right)/2LL;
				LL leftside = accum[center] - ((left_index==0)?0:accum[left_index-1]);
				LL rightside = accum[right_index] - accum[center];
				if(leftside==rightside) return center;
				if(leftside>rightside) right = center;
				if(leftside<rightside) left = center;
			}
			LL left_diff,right_diff;
			{
				LL leftside = accum[left] - ((left_index==0)?0:accum[left_index-1]);
				LL rightside = accum[right_index] - accum[left];
				left_diff = abs(rightside-leftside);
			}
			{
				LL leftside = accum[right] - ((left_index==0)?0:accum[left_index-1]);
				LL rightside = accum[right_index] - accum[right];
				right_diff = abs(rightside-leftside);
			}
			return (left_diff<right_diff)?left:right;
		};
		LL left = search(0,middle);
		LL right = search(middle+1,N-1);

		LL P = accum[left];
		LL Q = accum[middle] - accum[left];
		LL R = accum[right] - accum[middle];
		LL S = accum[N-1] - accum[right];
		LL cand = max(max(P,Q),max(R,S)) - min(min(P,Q),min(R,S));
		ans = min(ans,cand);
	}
}
 
int main(){
	cin >> N;
	for(int i=0;i<N;i++) cin >> A[i];
 
    solve();

	cout << ans << endl;
    return 0;
}