#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+50;
const ll inf=1e18;
inline int gi() {
  char ch=getchar(); int q=0,x=0;
  while(ch<'0'||ch>'9') q=(ch=='-'?1:q),ch=getchar();
  while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
  return q?(-x):x;
}
int n,a[N]; ll s[N],ans=inf;
int main(){
  cin>>n;
  for (int i=1; i<=n; ++i)
    a[i]=gi(),s[i]=s[i-1]+a[i];
  int l=1,r=3;
  for (int i=2; i<n-1; ++i){
    while(l<i)
      if (min(s[l],s[i]-s[l])<min(s[l+1],s[i]-s[l+1]))
	++l;
      else
	break;
    while(r<n)
      if (min(s[r]-s[i],s[n]-s[r])<min(s[r+1]-s[i],s[n]-s[r+1]))
	++r;
      else
	break;
    ans=min(ans,max(s[l],max(s[i]-s[l],max(s[r]-s[i],s[n]-s[r])))-min(s[l],min(s[i]-s[l],min(s[r]-s[i],s[n]-s[r]))));
  }
  cout<<ans;
  return 0;
}