#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn=2e5+5;
ll const INF=1e18;
ll a[maxn],b[maxn],res;
int n;
inline ll read(){
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}
ll get(int l,int pos,int r){
  //  cout<<b[l]<<" "<<b[pos]-b[l]<<" "<<b[r]-b[pos]<<" "<<b[n]-b[r]<<endl;
    return max(max(b[l],b[pos]-b[l]),max(b[r]-b[pos],b[n]-b[r]))-min(min(b[l],b[pos]-b[l]),min(b[r]-b[pos],b[n]-b[r]));
}
void dfs(int pos){
    int l=lower_bound(b+1,b+n+1,b[pos]>>1)-b-1;
    int r=lower_bound(b+1,b+n+1,((b[n]-b[pos])>>1)+b[pos])-b-1;
    //cout<<l<<" "<<pos<<" "<<r<<endl;
    res=min(res,get(l,pos,r));
    res=min(res,get(l+1,pos,r));
    res=min(res,get(l,pos,r+1));
    res=min(res,get(l+1,pos,r+1));
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        a[i]=read();
        b[i]=b[i-1]+a[i];
    }
    res=INF;
    int pos=lower_bound(b+1,b+n+1,b[n]>>1)-b-1;
    dfs(pos);
    dfs(pos+1);
    printf("%lld\n",res);
}
