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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Math;
class Z { static void Main() => new K(); }
class K
{
	int F => int.Parse(Str);
	long FL => long.Parse(Str);
	int[] G => Strs.Select(int.Parse).ToArray();
	long[] GL => Strs.Select(long.Parse).ToArray();
	string Str => ReadLine();
	string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
	const int MOD = 1000000007;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	void Solve()
	{
		var N = F;
		var A = G;
		var sum = new long[N + 1];
		for (var i = 0; i < N; i++) sum[i + 1] = sum[i] + A[i];
		// [0, N) => [0, m) + [m, N), where 2 <= m <= N-2
		var l = new int[N];	// 1 <= l <= m-1, [0, m) => [0, l) + [l, m) and |sum[0, l] - sum[l, m]| is minimum, then l[m] = l
		var r = new int[N];	// m+1 <= r <= N-1, [m, N) => [m, r) + [r, N) and |sum[m, r] - sum[r, N]| is minimum, then r[m] = r
		l[2] = 1; r[N - 2] = N - 1;
		for (var m = 3; m <= N - 2; m++)
		{
			var p = l[m - 1];
			var d = Abs(2 * sum[p] - sum[0] - sum[m]);
			var q = p + 1;
			while (q <= m - 1)
			{
				var e = Abs(2 * sum[q] - sum[0] - sum[m]);
				if (e > d) break;
				if (e < d) { d = e; p = q; }
				q++;
			}
			l[m] = p;
			// WriteLine($"l[{m}] = {l[m]}");
		}
		for (var m = N - 3; m >= 2; m--)
		{
			var p = r[m + 1];
			var d = Abs(2 * sum[p] - sum[m] - sum[N]);
			var q = p - 1;
			while (q >= m + 1)
			{
				var e = Abs(2 * sum[q] - sum[m] - sum[N]);
				if (e > d) break;
				if (e < d) { d = e; p = q; }
				q--;
			}
			r[m] = p;
			// WriteLine($"r[{m}] = {r[m]}");
		}
		var ans = long.MaxValue;
		for (var m = 2; m <= N - 2; m++)
		{
			var P = sum[l[m]] - sum[0];
			var Q = sum[m] - sum[l[m]];
			var R = sum[r[m]] - sum[m];
			var S = sum[N] - sum[r[m]];
			var t = new long[] {P, Q, R, S};
			ans = Min(ans, t.Max() - t.Min());
		}
		WriteLine(ans);
	}
}