#include"bits/stdc++.h"
using namespace std;
typedef long long ll;

#define N '\n'
#define S ' '
#define Z '0'
#define Co const
#define L ll
#define V vector<L>
#define VV vector<V>
#define LI list<L>
#define LL list<LI>
#define DQ deque<L>
#define DD deque<DQ>
#define AU auto
#define RT return
#define elif else if
#define TTA template<class T,class...A>
#define TT template<class T>
#define TA template<class...A>
#define Tt T t
#define Aa A...a
#define Tt2 T&&t
#define Aa2 A&&...a
#define M(a) move(a)...
#define IV inline void
#define IL inline L
#define IB inline bool
#define IVL inline V

#define A1(a,b) L a[b]
#define A2(a,b,c) L a[b][c]
#define V1(a,b) V a(b)
#define V2(a,b,c) VV a;a=VV(b,V(c,0))

#define W(a) while(a)
#define F(a,b,c) for(L a=(b);a<(c);++a)
#define R(a,b) for(L a=0;a<(b);++a)
#define RP(a,b) for(L a=(b);a>=0;++a)
#define FE(a,b) for(AU a:b)
#define FE2(a,b) FE(X,b)FE(a,X)
#define FA(a,b) for(AU&a:b)
#define FA2(a,b) FA(X,b)FA(a,X)

#define LF(a) a.front()
#define LB(a) a.back()
#define PF(a,b) a.push_front(b)
#define PB(a,b) a.push_back(b)
#define EF(a) a.pop_front()
#define EB(a) a.pop_back()
#define ES(a,b) a.erase(b)
#define ES2(a,b,c) a,erase(a,b,c)
#define IS(a,b) a.insert(b)
#define FD find

#define VI(a,b) R(I,b)cin>>X,PSB(a,X)
#define FI(a) FA(X,a)I(X)
#define F2I(a) FA2(Y,a)I(Y)
#define FO(a) FE(X,a)O(X)
#define OutSet(a,b) K=a,E=b
#define FP(a) J=1;FE(X,a){if(J){cout<<X;J=0;}else cout<<K<<X;}cout<<E
#define F2O(a) FE(Y,a){J=1;FE(X,Y){if(J){cout<<X;J=0;}else cout<<K<<X;}cout<<E;}

#define AZ(a) sizeof(a)/sizeof(a[0])
#define VZ(a) a.size()
#define AT(a) a,a+AZ(a)
#define VT(a) a.begin(),a.end()
#define AX(a) *max_element(AT(a))
#define AN(a) *min_element(AT(a))
#define VX(a) *max_element(VT(a))
#define VN(a) *min_element(VT(a))
#define AA(a) accumulate(AT(a),0)
#define VA(a) accumulate(VT(a),0)
#define AF(a,b) FD(AT(a),b)
#define VF(a,b) FD(VT(a),b)
#define AS(a) sort(AT(a))
#define VS(a) sort(VT(a))
#define LS(a) a.sort()

#define P(a) cout<<#a<<N
#define F10(a) cout<<fixed<<setprecision(10)<<a
#define FC(a,b,c) cout<<setfill(a)<<setw(b)<<right<<c

#define Q cin.tie(0);ios::sync_with_stdio(false)

Co L P[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
char K=S,E=N;
bool J;

IV I(){}TTA IV I(Tt2,Aa2){cin>>t;I(M(a));}
TT IV O(Tt){cout<<t<<K;}TTA IV O(Tt,Aa){cout<<t<<E;O(M(a));}

IL dig(L a){L d=1;W(a/=10)++d;RT d;}
IL gcd(L a,L b){L c;W(a){c=a;a=b%a;b=c;}RT b;}
IB isp(L a){
	if(a<48){F(i,0,15)if(P[i]==a)RT 1;}
	elif(a<2809)RT gcd(614889782588491410,a)==1;
	else {P(Error:Argument>2808);exit(1);}RT 0;}

class comp{public:Co V&u;
	comp(Co V&v):u(v){}
	bool operator()(L a,L b){RT u[a]<u[b];}};
IV S2V(V&a,V&b){
	L n=VZ(a);V p(n),a2(n),b2(n);R(i,n)p[i]=i;
	sort(VT(p),comp(a));
	R(i,n)a2[i]=a[p[i]],b2[i]=b[p[i]];a=a2,b=b2;}
IV VVS(VV&a,L b){
	L m=VZ(a),n=VZ(a[0]);V p(n);V2(a2,m,n);
	if(b>=m||b<0){P(Error:Out of Range);exit(1);}
	R(i,n)p[i]=i;sort(VT(p),comp(a[b]));
	R(i,m)R(j,n)a2[i][j]=a[i][p[j]];
	a=a2;}
IVL AFD(V a,L c){
	V b;
	R(i,VZ(a))if(a[i]==c)PB(b,i);
	RT b;
}
IVL VRS(V a){
	V b(VZ(a));
	b[0]=a[0];
	F(i,1,VZ(a))b[i]=a[i]+b[i-1];
	RT b;
}
IV RS(L*a,L*b){
	b[0]=a[0];
	F(i,1,AZ(a))b[i]=a[i]+b[i-1];
}

#define MM max(max(B[fi-1],B[i-1]-B[fi-1]),max(B[bi-1]-B[i-1],B[a-1]-B[bi-1]))-min(min(B[fi-1],B[i-1]-B[fi-1]),min(B[bi-1]-B[i-1],B[a-1]-B[bi-1]))

IV perm(void a(V),V b){VS(b);do{a(b);}W(next_permutation(VT(b)));}
//in main: perm([](V a){FP(a);},A);

int main(){
	Q;L a,b,c,d;
	
	I(a);
	V A(a);
	FI(A);
	V B=VRS(A);
	L s=10000000000000000,fi=1,bi=3;
	F(i,2,a-1){
		if(i==bi)++i;
		W(B[fi-1]<B[i-1]-B[fi-1]&&fi<i-1)++fi;
		W(B[bi-1]-B[i-1]<B[a-1]-B[bi-1]&&bi<a-1)++bi;
		if(i+1<bi)--bi;
		s=min(s,MM);
		if(fi>1)--fi;
		s=min(s,MM);
		if(bi<a-1)++bi;
		s=min(s,MM);
		if(fi<i-1)++fi;
		s=min(s,MM);
	}
	OutSet(S,N);
	O(s);
}

Measured memory_bytes: 4992