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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <string>
#include <utility>
#include <cassert>
#include <functional>
#include <numeric>
#include <chrono>
#include <random>
#include <optional>

using namespace std;
typedef long long ll;

const int MAXN = 2e5+10;
int n;
ll preSum[MAXN], a[MAXN];

// [i, j]
inline ll sumRange(int i, int j) {
  assert(i <= j);
  return i == 0 ? preSum[j] : preSum[j] - preSum[i-1];
}

ll getBest(ll k) {
  ll sum = sumRange(0, k);
  int lo = 0;
  int hi = k;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (sumRange(0, mid) >= sum/2) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  int i = hi;

  sum = sumRange(k+1, n-1);
  lo = k+1;
  hi = n-1;
  while (hi - lo > 1) {
    int mid = (hi + lo) / 2;
    if (sumRange(k+1, mid) >= sum/2) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  int j = hi;

  ll res = 1e18;
  // cerr << "i: " << i << " k: " << k << " j: " << j << endl;
  for (int izq = i-2; izq <= i+1; izq++) {
    for (int der = j-2; der <= j+1; der++) {
      if (izq > 0 && der > k+1 && izq <= k && der <= n-1) {
        ll P = sumRange(0, izq-1);
        ll Q = sumRange(izq, k);
        ll R = sumRange(k+1, der-1);
        ll S = sumRange(der, n-1);
        ll mx = max({P, Q, R, S});
        ll mn = min({P, Q, R, S});
        res = min(res, mx - mn);
        // cerr << izq << "  " << der << endl;
        // cerr << P << " " << Q << " " << R << " " << S << endl;
        // cerr << mx - mn << endl;
      }
    }
  }
  return res;
}


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  preSum[0] = a[0];
  for (int i = 1; i < n; i++) preSum[i] = preSum[i-1] + a[i];

  ll ans = 1e18;
  for (int k = 1; k < n-2; k++) {
    ll op = getBest(k);
    ans = min(ans, op);
  }
  cout << ans << endl;
  return 0;
}