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 <bits/stdc++.h>
using namespace std;

typedef long long ll;
 
using tpl = tuple<int, int, int>;
typedef pair<int,int> pii;

void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}

int N;
ll A[200010];
ll sums[200010];

void solve1(){
    ll ans = 1LL << 60;
    // [0, i) [i, N) に分ける
    for(int i = 2; i < N - 1; i++){
        vector<int> bef, aft;
        {
            int lb = -1;
            int ub = i;
            while(ub - lb > 1){
                int mid = (ub + lb) / 2;
                ll a = sums[mid + 1];
                ll b = sums[i] - sums[mid];
                if(a >= b) ub = mid;
                else lb = mid;
            }
            if(ub > 0) bef.push_back(ub - 1);
            bef.push_back(ub);
        }
        {
            int lb = i - 1;
            int ub = N;
            while(ub - lb > 1){
                int mid = (ub + lb) / 2;
                ll a = sums[mid + 1] - sums[i];
                ll b = sums[N] - sums[mid];
                if(a >= b) ub = mid;
                else lb = mid;
            }
            if(ub > i) aft.push_back(ub - 1);
            aft.push_back(ub);
        }
        for(int b : bef){
            for(int a : aft){
                int f1 = 0;
                int t1 = b;
                int f2 = b + 1;
                int t2 = i - 1;
                int f3 = i;
                int t3 = a;
                int f4 = a + 1;
                int t4 = N - 1;
                ll v1 = sums[t1 + 1] - sums[f1];
                ll v2 = sums[t2 + 1] - sums[f2];
                ll v3 = sums[t3 + 1] - sums[f3];
                ll v4 = sums[t4 + 1] - sums[f4];
                ll vs[] = {v1, v2, v3, v4};
                sort(vs, vs + 4);
                ans = min(ans, vs[3] - vs[0]);
            }
        }
    }
    cout << ans << endl;

}

void solve2(){
    ll ans = 1LL << 60;
    // [0, i) [i, N) に分ける
    int lpos = 0;
    int rpos = 2;
    for(int i = 2; i < N - 1; i++){
        rpos = max(i, rpos);
        ll v1 = -1;
        ll v2 = -1;
        ll v3 = -1;
        ll v4 = -1;
        // 一番左と次の総和が、一番近くなるところを求める
        while(1){
            ll a1 = sums[lpos + 1];
            ll b1 = sums[i] - sums[lpos + 1];
            v1 = a1; v2 = b1;
            if(lpos >= i - 2) break;
            ll a2 = sums[lpos + 2];
            ll b2 = sums[i] - sums[lpos + 1];
            if(abs(a1 - b1) >= abs(a2 - b2)){
                lpos++;
            }
            else break;
        }
        while(1){
            ll a1 = sums[rpos + 1] - sums[i];
            ll b1 = sums[N] - sums[rpos + 1];
            v3 = a1; v4 = b1;
            if(rpos >= N - 2) break;
            ll a2 = sums[rpos + 2] - sums[i];
            ll b2 = sums[N] - sums[rpos + 2];
            if(abs(a1 - b1) >= abs(a2 - b2)){
                rpos++;
            }
            else break;
        }
        ll vs[] = {v1, v2, v3, v4};
        sort(vs, vs + 4);
        ans = min(ans, abs(vs[0] - vs[3]));
    }
    cout << ans << endl;
}

int main(){

    cin >> N;
    for(int i = 0; i < N; i++) cin >> A[i];
    for(int i = 0; i < N; i++) sums[i + 1] = sums[i] + A[i];
    solve2();

 
    return 0;
}