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 FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 100010;
const int MAXK = 510;
const int MAXQ = 200010;

int a[MAXN], n;
ll l[MAXN], r[MAXN];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n;
    FOR(i,1,n) cin >> a[i];
    FOR(i,1,n) l[i] = l[i-1] + a[i];
    FORE(i,n,1) r[i] = r[i+1] + a[i];
    int lefNotch = 1, rigNotch = 3;
    ll res = 1e18;
    FOR(i,2,n-2) {
        ll sumLef = l[i];
        ll sumRig = r[i+1];

        ll sumLefLef = l[lefNotch];
        ll sumLefRig = sumLef - sumLefLef;
        ll diffLef = sumLefLef - sumLefRig;
        while (lefNotch + 1 < i) {
            int newLefNotch = lefNotch + 1;

            ll newSumLL = l[newLefNotch];
            ll newSumLR = sumLef - newSumLL;
            ll newDiffLef = newSumLL - newSumLR;

            if (abs(diffLef) <= abs(newDiffLef)) break;

            diffLef = newDiffLef;
            lefNotch = newLefNotch;
        }

        rigNotch = max(rigNotch, i+1);
        ll sumRigRig = r[rigNotch+1];
        ll sumRigLef = sumRig - sumRigRig;
        // cout << sumRigLef << ' ' << sumRigRig << endl;
        ll diffRig = sumRigLef - sumRigRig;
        while (rigNotch + 1 < n) {
            int newRigNotch = rigNotch + 1;

            ll newSumRR = r[newRigNotch+1];
            ll newSumRL = sumRig - newSumRR;
            ll newDiffRig = newSumRL - newSumRR;

            // cout << newSumRL << ' ' << newSumRR << endl;

            if (abs(diffRig) <= abs(newDiffRig)) break;

            diffRig = newDiffRig;
            rigNotch = newRigNotch;
        }

        sumLefLef = l[lefNotch];
        sumLefRig = sumLef - sumLefLef;
        sumRigRig = r[rigNotch+1];
        sumRigLef = sumRig - sumRigRig;
        // cout << i << ' ' << sumLefLef << ' ' << sumLefRig << ' ' << sumRigLef << ' ' << sumRigRig << "\n";
        ll Min = min(sumLefLef, min(sumLefRig, min(sumRigRig, sumRigLef)));
        ll Max = max(sumLefLef, max(sumLefRig, max(sumRigRig, sumRigLef)));
        res = min(res, Max - Min);
    }
    cout << res;
    return 0;
}