//dile a la jardinera que traigo flores
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0);cin.tie(0)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz(x) int((x).size())
#define all(v) (v).begin(),(v).end()
#define trace(x) cout << #x << " = " << x << endl
#define For(i,a,b) for(int i = int(a); i < int(b); i++)
#define roF(i,a,b) for(int i = int(a); i >= int(b); i--)
using namespace std;

typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const int N=2e5+100;
ll n,mm[N],sum[N];

ll uwu(int l,int r){
	ll ans=sum[r];
	if(l)ans-=sum[l-1];
	return ans;
}

ll go(int l,int r){

}

ii eval(int ini,int fin){
	ll dif=LLONG_MAX;
	int L=ini,R=fin-1;
	while(R-L>7){
		int mid1=L+(R-L)/3;
		int mid2=R-(R-L)/3;
		if(go(ini,mid1)<=go(ini,mid2))R=mid2;
		else L=mid1;
	}
	ii ans;
	For(i,L,R+1){
		ll val=go(ini,i);
		if(val>dif)continue;
		dif=val;
		bloq1=uwu(ini,i);
		bloq2=uwu(i+1,fin-1);
		ii ans={max(bloq1,bloq2),min(bloq1,bloq2)};
	}
	return ans;
}

int main(){
	scanf("%lld",&n);
	For(i,0,n)scanf("%lld",&mm[i]);
	sum[0]=mm[0];
	For(i,1,n)sum[i]+=sum[i-1];
	ll ans=LLONG_MAX;
	For(i,2,n-1){
		ii par1=eval(0,i);
		ii par2=eva(i,n);
		ll mx=max(par1.ff,par2.ff);
		ll mn=min(par1.ss,par2.ss);
		ans=min(ans,mx-mn);
	}
	printf("%lld\n",ans);
	return 0;
}