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: Wrong Answer)

#include<iostream>
#include<algorithm>
#include<set>
#include<math.h>
#include<vector>
#include<sstream>
#include<queue>
#include<functional>
#include<bitset>
#include<cstdio>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include <string.h>
using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define reps(i,x) for(ll i=1;i<=(ll)(x);i++)
#define rrep(i,x) for(ll i=((ll)(x)-1);i>=0;i--)
#define rreps(i,x) for(ll i=(ll)(x);i>0;i--)
#define all(x) (x).begin(),(x).end()
#define m0(x) memset(x,0,sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll,ll>>
#define vpi vector<pair<int,int>>
#define mod 1000000007 
using namespace std;
int a[200005];
ll dp[200005];
ll b[4];
int main() {
	int n;
	cin >> n;
	rep(i , n)
		cin >> a[i];
	dp[0] = a[0];
	reps(i, n-1){
		dp[i] = dp[i-1] + a[i];	
	}
	ll ans = 1 << 30;
	int lf, rt;
	lf = 0;
	rt = 2;
	for(int i = 2; i < n-1; i++){
		ll lmn = 1 << 30;
		ll rmn = 1 << 30;
		for(int j = lf; j < i - 1; j++){
			if(lmn > abs(dp[j] -abs(dp[i-1] - dp[j]))){
				b[0] = dp[j];
				b[1] = (i-1 == j ? dp[i-1] : dp[i-1] -dp[j]);
				lmn =abs(dp[j] -abs(dp[i-1] - dp[j]));
				lf = j;
			}else{break;}
		}
			if(i == n - 1 && rmn > abs(dp[n-2] - dp[n-3] -( dp[n-1] -dp[n-2]))){
				b[2] = dp[n-2] - dp[n-3];
				b[3] = dp[n-1] - dp[n-2];
			}else{
		for(int j = rt; j < n; j++){
			if(rmn > abs(abs(dp[j] - dp[i-1]) -abs(dp[n-1] - dp[j]))){
				b[2] = dp[j] - dp[i-1];
				b[3] = dp[n-1] - dp[j];
				rmn = abs(abs(dp[j] - dp[i-1]) -abs(dp[n-1] - dp[j]));
				rt  = j;
			}else{break;}
		}
            }
		sort(b, b+4);
		ans = min(ans, b[3] - b[0]);
	}
	cout << ans << endl;
}