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>

#define mp make_pair
#define pb push_back

using namespace std;

typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int MAX_N = 200000;
int n;
i64 a[MAX_N];
i64 psum[MAX_N+1];

int main()
{
  cin >> n;
  psum[0] = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    psum[i+1] = psum[i] + a[i];
  }

  i64 m = LLONG_MAX;
  // i: 右端(含む)
  for (int i = 1; i < n-2; i++) {
    i64 t = psum[i+1];
    int j = upper_bound(psum, psum+i+2, t/2) - psum;
    pair<i64,i64> p1 = minmax(psum[j], t - psum[j]);
    if (j > 0) {
      auto p2 = minmax(psum[j-1], t - psum[j-1]);
      if (p1.second - p1.first > p2.second - p2.first)
        p1 = p2;
    }
    //printf("p1={%ld, %ld}\n", p1.first, p1.second);

    i64 u = psum[i+1] + (psum[n] - psum[i+1]) / 2;
    int k = upper_bound(psum+i+2, psum+n+1, u) - psum;
    pair<i64,i64> p2 = minmax(psum[k] - psum[i+1], psum[n] - psum[k]);
    if (k > i+2) {
      auto p3 = minmax(psum[k-1] - psum[i+1], psum[n] - psum[k-1]);
      if (p2.second - p2.first > p3.second - p3.first)
        p2 = p3;
    }
    //printf("p2={%ld, %ld}\n", p2.first, p2.second);
    //printf("i=%d j=%d k=%d\n", i, j, k);

    m = min(m, max(p1.second, p2.second) - min(p1.first, p2.first));
  }

  cout << m << endl;

  return 0;
}