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 <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
#include <cassert>
//#include <numeric>
#pragma warning(disable:4996) 
 
typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;

void calc(int i, const ll* s, ll& min00, ll& max00)
{
    assert(i >= 2);
    min00 = LINF; max00 = -LINF;

    ll tmp0 = s[0]+(s[i]-s[0]) / 2;
    int k = lower_bound(s + 0, s + i, tmp0) - s;
    ll  min0 = LINF;
    int mini0 = 0;
    if (k - 1 >= 1 && k-1 < i) {
        ll tmp = MAX(s[k-1]-s[0], s[i] - s[k-1]) - MIN(s[k-1]-s[0], s[i] - s[k-1]);
        if (tmp < min0) {
            min0 = tmp;
            mini0 = k - 1;
        }

    }
    if (k>=1 &&  k< i) {
        ll tmp = MAX(s[k]-s[0], s[i] - s[k]) - MIN(s[k]-s[0], s[i] - s[k]);
        if (tmp < min0) {
            min0 = tmp;
            mini0 = k;
        }
    }
    if (min0 < LINF) {
        min00 = MIN(s[mini0]-s[0], s[i] - s[mini0]);
        max00 = MAX(s[mini0]-s[0], s[i] - s[mini0]);
    }
    return;
}

void solve()
{
    int n;
    scanf("%d", &n);
    vector<ll> a(n);
    vector<ll> s(n+1);
    int i;
    for (i = 0; i < n; i++) {
        scanf("%lld", &a[i]);
        s[i + 1] = s[i] + a[i];
    }
    ll ans = LINF;
    for (i = 2; i <= n-2; i++) {
        ll min0, max0, min1, max1;
        calc(i, &s[0], min0, max0);
        calc(n-i, &s[i], min1, max1);

        ll tmp = MAX(max0, max1) - MIN(min0, min1);
        ans = MIN(ans, tmp);
    }
    printf("%lld\n", ans);

    return;
}

int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}