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 ff(i, x, y) for(int i = x;i <= y;i++)
using namespace std;
typedef long long ll;
int posl, posr;
ll  pre[200010], mes[200010], n, now;
int checkl(int mid, int i) {
	ll val = (pre[mid] - (pre[i - 1] - pre[mid]));
	if(abs(val) < now) {
		posl = mid;
		now = abs(val);
	}
	if(val < 0)
		return -1;
	if(val == 0)
		return 0;
	return 1;
}
bool checkr(int mid, int i) {
	ll val = (pre[n] - pre[mid - 1] - (pre[mid - 1] - pre[i - 1]));
	if(abs(val) < now) {
		posr = mid;
		now = abs(val);
	}
	if(val < 0)
		return -1;
	if(val == 0)
		return 0;
	return 1;
}
int main() {
	scanf("%lld", &n);
	ff(i, 1, n) {
		scanf("%lld", &mes[i]);
		pre[i] = pre[i - 1] + mes[i];
	}
	ll ans = 1e16;
	ff(i, 3, n - 2) {
		posl = 1; 
		posr = n;
		ll l, r, mid;
		l = 1; r = i - 2; now = abs(pre[posl] - (pre[i - 1] - pre[posl]));
		while(l <= r) {
			mid = (l + r) / 2;
			int flag = checkl(mid, i);
			if(flag == 0)
				break;
			else if(flag > 0) 
				r = mid - 1;
			else
				l = mid + 1;
			//if(i == 5)
			//	printf(" %d \n", );
		}
		l = i + 1; r = n; now = abs(mes[n] - (pre[n - 1] - pre[i - 1]));
		while(l <= r) {
			mid = (l + r) / 2;
			int flag = checkr(mid, i);
			if(flag == 0)
				break;
			else if(flag > 0)
				l = mid + 1;
			else
				r = mid - 1;
		}
		ll mx = max(max(pre[posl], pre[i - 1] - pre[posl]), max(pre[n] - pre[posr - 1], pre[posr - 1] - pre[i - 1]));
		ll mi = min(min(pre[posl], pre[i - 1] - pre[posl]), min(pre[n] - pre[posr - 1], pre[posr - 1] - pre[i - 1]));
		ans = min(ans, mx - mi);
	}
	printf("%lld\n", ans);
	return 0;
}