#include<stdio.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 200005

int n;
ll s[N],ans=1e18;

int main()
{
	scanf("%d",&n);
	for (int i=1,a;i<=n;i++) scanf("%d",&a),s[i]=s[i-1]+a;
	for (int mid=2,L=1,R=3;mid<n-1;mid++)
	{
		while(abs(s[mid]-2*s[L])>abs(s[mid]-2*s[L+1])) L++;
		while(abs(s[n]-2*s[R]+s[mid])>abs(s[n]-2*s[R+1]+s[mid])) R++;
		ans=min(ans,max(max(s[n]-s[R],s[R]-s[mid]),max(s[mid]-s[L],s[L]))-min(min(s[n]-s[R],s[R]-s[mid]),min(s[mid]-s[L],s[L])));
	}
	printf("%lld\n",ans);
}