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>

#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define ull unsigned long long int
#define lli long long int
#define li long int

#define si(n) scanf("%d",&n)
#define sl(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)

#define pi(n) printf("%d",n)
#define pl(n) printf("%ld",n)
#define pll(n) printf("%lld",n)

using namespace std;

const int MAX=2e5+5;

lli A[MAX], cum[MAX];

int part(int start, int last) {
    int L = start, R = last;
    lli deduct = cum[start-1], least = (lli)(1e15); int ind = -1;

    //bool debug = 0; if(start == 5 || last == 5) { debug = 1;}
    //if(debug)
        //cout << "start = " << start << "\tlast = " << last <<  "\tDeduct = " << deduct << "\n";
    while(start<last) {
        int mid = (start+last)>>1;
        lli Leftval = cum[mid]-deduct, Rightval = cum[R]-cum[mid];
        //if(debug)
            //cout << "start = " << start << "\tlast = " << last << "\tLeftval = " <<  Leftval << "\tRightval = " << Rightval << "\n";
        if(cum[mid]-deduct == cum[R]-cum[mid])
            return mid;
        if(cum[mid]-deduct < cum[R]-cum[mid]) {
            if(least > abs(cum[R]-2*cum[mid]+deduct)) {
                least = abs(cum[R]-2*cum[mid]+deduct); ind = mid;
            }
            start = mid+1;
        }
        else {
            if(least > abs(cum[R]-2*cum[mid]+deduct)) {
                least = abs(cum[R]-2*cum[mid]+deduct); ind = mid;
            }
            last = mid-1;
        }
    }
    //cout << "Least = " << least << "\tFinal start = " << start << "\tFinal last = " << last << "\n";
    lli other = (lli)(1e15);
    if(start == last) {
        other = abs(cum[start]-cum[L-1] - (cum[R]-cum[start]));
        //cout << "Start = Last \tother = " << other << "\n";
    }
    if(other < least) {
        ind = start; least = other;
    }
    if(start == last) {
        other = abs(cum[start-1]-cum[L-1] - (cum[R]-cum[start-1]));
        //cout << "Start = Last \tother = " << other << "\n";
    }
    //cout << L << " " << R << " ind = " << ind << "\n";
    if(other < least)
        ind = start-1;
    return ind;
}

int main() {
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    int N;
    cin >> N;
    cum[0] = 0;
    for(int i=1; i<=N; i++) {
        cin >> A[i]; cum[i] = cum[i-1] + A[i];
    }

    lli ans = (lli)(1e15);
    for(int i=2; i<N; i++) {
        int j = part(1,i);
        //cout << "-----------\n";
        int k = part(i+1,N);
        lli B = cum[j], C = cum[i]-cum[j], D = cum[k]-cum[i], E = cum[N]-cum[k];
        lli mx = max(B,max(C, max(D,E))), mn = min(B, min(C,min(D,E)));
        //cout << j << "\t" << i << "\t" << k << "\n";

        if(ans > (mx-mn)) {
            ans = mx-mn;
        }
    }

    cout << ans << "\n";



    return 0;
}