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)

#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define re return
#define ll long long
#define ull unsigned ll
#define ld long double
#define f first
#define s second
#define pi acos(-1)
#define oo (ll)1e9+8
#define OO 1e18
#define EPS 1e-6
#define Endl '\n'
#define all(v) (v).begin(),(v).end()
#define FX(n) fixed<<setprecision(n)
#define mm(o,k) memset(o,k,sizeof o)
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
const int N=2e5+5,M=N*2,Q=4e5;
int n,akbr[4];
ll a[N],mn(OO),tot;
void grb(){
    ll sum=0;
    int fen=0;
    vector<ll> v;
    for(int i=0;i<n;++i){
        /*if(i==n-1&&fen==1){
            v.push_back(sum);
            v.push_back(a[i]);
            sum=0;
            break;
        }*/
        if(n-i==4-fen-1){
            v.push_back(sum);
            sum=0;
            ++fen;
        }
        sum+=a[i];
        if(sum==tot){
            v.push_back(sum);
            sum=0;
            ++fen;
        }
        else if(sum>tot){
            //if(akbr[fen]&&!fen)cout<<sum<<endl;
            if(akbr[fen])v.push_back(sum),sum=0;
            else v.push_back(sum-a[i]),sum=a[i];
            ++fen;
        }
    }
    if(sum)v.push_back(sum);
    sort(v.begin(),v.end());
    if(v.size()!=4)re ;
    //for(int i=0;i<4;++i)cout<<akbr[i]<<" ";cout<<endl;
    //for(auto i:v)cout<<i<<" ";cout<<endl;
    mn=min(mn,abs(v[0]-v[3]));
    re ;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;++i)
        scanf("%lld",a+i),tot+=a[i];
    tot/=4ll;
    //cout<<tot<<endl;
    for(int msk=0;msk<16;++msk){
        for(int j=0;j<4;++j)
            akbr[j]=(msk&(1<<j))>0;
        grb();
    }
    printf("%lld\n",mn);
    re 0;
}