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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
int main(){
    ll n; cin>>n;
    vector<ll> a(n);
    rep(i,n) cin>>a[i];
    vector<ll> cum(n);
    rep(i,n){
        if(i==0) cum[i]=a[i];
        else cum[i]=cum[i-1]+a[i];
    }
    ll res=1e17;
    for(ll M=1; M<n-2; M++){ //a[0]...a[M] / a[M+1]...a[n-1]
        ll p,q,r,s;
        ll left=0, right=M-1;
        while(right-left>1){
            //a[0]...a[mid] / a[mid+1]...a[M]
            //cum[mid] / cum[M]-cum[mid]
            //-mid ~ cum[M]-cum[mid]*2
            ll mid=(left+right)/2;
            if(cum[M]-2*cum[mid]>0) left=mid;
            else right=mid;
        }
        if(abs(cum[M]-2*cum[left]) < abs(cum[M]-2*cum[right])){
            p=cum[left]; q=cum[M]-cum[left];
        }else{
            p=cum[right]; q=cum[M]-cum[right];
        }
        
        left=M+1, right=n-2;
        while(right-left>1){
            //a[M+1]...a[mid] / a[mid+1]...a[n-1]
            //cum[mid]-cum[M+1] / cum[n-1]-cum[mid]
            //-mid ~ cum[n-1]-2*cum[mid]+cum[M+1]
            ll mid=(left+right)/2;
            if(cum[n-1]-2*cum[mid]+cum[M]>0) left=mid;
            else right=mid;
        }
        if(abs(cum[n-1]-2*cum[left]+cum[M]) < abs(cum[n-1]-2*cum[right]+cum[M])){
            r=cum[left]-cum[M]; s=cum[n-1]-cum[left];
        }else{
            r=cum[right]-cum[M]; s=cum[n-1]-cum[right];
        }

        vector<ll> V={p,q,r,s};
        sort(V.begin(), V.end());
        chmin(res, V[3]-V[0]);
    }
    cout<<res<<endl;
    return 0;
}