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 <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}


int main(){
    int n;
    cin >> n;
    vector<ll>a(n),s(n);
    rep(i,n){
        cin >> a[i];
        s[i] += a[i];
        if(i!=0)s[i] += s[i-1];
    }
    ll ans = 1LL<<60;
    for(int i=1;i<n-1;i++){
        vector<ll> p;
        ll tmp1,tmp2;
        ll x = s[i];
        ll tx = (x+1)/2;
        ll ind = lower_bound(s.begin(),s.end(),tx)-s.begin();
        ll pp,qq;
        if(ind==0){
            pp = s[0];
            qq = s[i]-s[0];
        }else{
            pp = s[ind-1];
            qq = s[i]-s[ind-1];
            if(abs(pp-qq)>abs(s[ind]-(s[i]-s[ind]))){
                pp = s[ind];
                qq = s[i]-s[ind];
            }
        }
        p.PB(pp);p.PB(qq);
        ll y = s[n-1]-s[i];
        ll ty = s[i] +  (y+1)/2;
        ind = lower_bound(s.begin(),s.end(),ty)-s.begin();
        if(ind==i+1){
            pp = s[i+1]-s[i];
            qq = s[n-1]-s[i+1];
        }else{
            pp = s[ind-1]-s[i];
            qq = s[n-1]-s[ind-1];
            if(abs(pp-qq)>abs(s[ind]-s[i]-(s[n-1]-s[ind]))){
                pp = s[ind]-a[i];
                qq = s[n-1]-s[ind];
            }
        }
        p.PB(pp);p.PB(qq);
        sort(p.begin(),p.end());
        ans = min(ans,p[3]-p[0]);
    }
    cout << ans << endl;
    return 0;
}