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 <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
 
int N, A[200010];

// [l, r) をふたつにきる
pair<int, int> solve(int l, int r) {
    // 総和
    int sum = A[r-1] - A[l-1];
    int idx = lower_bound(A+l, A+r, A[l-1] + sum/2) - A;
    // fprintf(stderr, "solve [%lld, %lld): sum = %lld, idx = %lld\n", l, r, sum, idx);


    int minDiff = INF;
    pair<int, int> ans;
    for(int k=-1; k<1; k++) {
        if(idx + k < l or idx + k >= r-1) continue;
        int nidx = idx + k;
        int sumA = A[nidx] - A[l-1];
        int sumB = A[r-1] - A[nidx];
        if(minDiff > abs(sumA - sumB)) {
            minDiff = abs(sumA - sumB);
            if(sumA > sumB) swap(sumA, sumB);
            ans = make_pair(sumA, sumB);
        }
    }
    return ans;
}

signed main() {
    cin >> N;
    for(int i=0; i<N; i++) {
        cin >> A[i+1];
        A[i+1] += A[i];
    }

    // 半分にきるところ (i と i+1 の間できる)
    int ans = INF;
    for(int i=2; i<=N-2; i++) {
        int mia, maa, mib, mab;
        tie(mia, maa) = solve(1, i+1);
        tie(mib, mab) = solve(i+1, N+1);
        // fprintf(stderr, "mia = %lld, maa = %lld, mib = %lld, mab = %lld\n", mia, maa, mib, mab);
        chmin(ans, max(maa, mab) - min(mia, mib));
    }
    cout << ans << endl;
    return 0;
}