#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int n;
ll l, r, ans = 1e18 + 1;
ll a[maxn], s[maxn], t[4];
ll f(ll x) { return x > 0 ? x : -x; }
int main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n; l = 1, r = 3;
	for (int i = 1; i <= n; i++)cin >> a[i], s[i] = s[i - 1] + a[i];
	for (int i = 2; i <= n - 2; i++) {
		while (l + 1 < i && f(2 * s[l] - s[i])
			>= f(2 * s[l + 1] - s[i])) l++;
		while (r + 1 < n && f(2 * s[r] - s[i] - s[n])
			>= f(2 * s[r + 1] - s[i] - s[n])) r++;
		t[0] = s[l];
		t[1] = s[i] - s[l];
		t[2] = s[r] - s[i];
		t[3] = s[n] - s[r];
		sort(t, t + 4);
		ans = min(ans, t[3] - t[0]);
	}
	cout << ans;
	return 0;
}