#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
long long N,x;
long long A[200005],Ans=1e18;
inline void read(long long &Res){
	char ch=getchar();
	for (Res=0;!isdigit(ch);ch=getchar());
	for (;isdigit(ch);ch=getchar()) Res=(Res<<3)+(Res<<1)+ch-48;
}
inline long long check(long long a,long long b,long long c,long long d){
	return max(max(a,b),max(c,d))-min(min(a,b),min(c,d));
}
int main(){
	read(N);
	for (int i=1;i<=N;++i) read(x),A[i]=A[i-1]+x;
	for (int i=2;i<N-1;++i){
		long long LL=std::upper_bound(A+1,A+i,A[i]/2.0)-A;
		long long RR=std::upper_bound(A+i+1,A+N+1,(A[i]+A[N])/2.0)-A;
		for (int L=LL-1;L<=LL;++L)
			for (int R=RR-1;R<=RR;++R){
				if (0<L&&L<i&&i<R&&R<N) Ans=min(Ans,check(A[i]-A[L],A[L],A[N]-A[R],A[R]-A[i]));
			}
	}
	printf("%lld\n",Ans);
	return 0;
}
