#include <bits/stdc++.h>
#define LL long long
#define INF 1e18
#define MAXN 200005
using namespace std;

int n;
LL a[MAXN], ans=INF;

int main()
{
	scanf("%d", &n);
	for(int i=1; i<=n; i++)	scanf("%lld", &a[i]), a[i]+=a[i-1];
	int l=1, r=3;
	LL maxn, minn;
	for(int i=2; i<=n-2; i++)
	{
		while(a[l]<a[i]-a[l]) l++;
		while(a[r]-a[i]<a[n]-a[r]) r++;
		if(a[l-1]>a[i]-a[l]) l--;
		if(a[r-1]-a[i]>a[n]-a[r]) r--;
		maxn=max(max(a[l], a[i]-a[l]), max(a[r]-a[i], a[n]-a[r]));
		minn=min(min(a[l], a[i]-a[l]), min(a[r]-a[i], a[n]-a[r]));
		ans=min(ans, maxn-minn);
	}
	printf("%lld\n", ans);
}