#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
// #include <boost/multiprecision/cpp_int.hpp> 

#define gc getchar//_unlocked
#define pc putchar//_unlocked
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pp pair<int,int>
#define ppl pair<ll,ll>
#define bigint boost::multiprecision::cpp_int
#define finp ios_base::sync_with_stdio(0);cin.tie(0);
#define bc __builtin_popcountll
#define afor(i,a,b) for(int i=a;i<=b;++i)
#define bfor(i,a,b) for(int i=a;i>=b;--i)
#define vi vector<int>
#define vpp vector<pp>
#define vll vector<ll>
#define fr first
#define se second

using namespace std;
using namespace __gnu_pbds;

char putnb[20];
void putn(ll n) {if(!n)pc('0');if(n<0)pc('-'),n=0-n;int pi=0;while(n)putnb[pi++]=(n%10)+'0',n/=10;while(pi)pc(putnb[--pi]);}
void sci(int *x) {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x = (int)((((*x)<<1) + ((*x)<<3)) + c - 48);}
void scll(ll *x)  {register char c = gc();*x = 0;for(; (c<48)||(c>57);c = gc());for(; (c>47)&&(c<58);c = gc())*x =  (ll)((((*x)<<1) + ((*x)<<3)) + c - 48);}
ll fp(ll a,ll b,ll c) {if(b==0)return 1%c; if(b==1)return a%c; ll ret=fp(a,b/2,c); ret=(ret*ret)%c; if(b&1)ret=(ret*a)%c; return ret;}

const ll mod=1e9 +7;
const ll mod2=1999999973;
const ll inf=1e18;
const int infs=1e9 + 1000;
const int N=200000;
const long double PI = acos(-1);

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


ll n;
ll A[N + 5];
ll cum[N + 5];

int main()
{
  finp;
  cin>>n;
  afor(i,1,n)
  {
    cin>>A[i];
    cum[i] = A[i] + cum[i-1];
  }

  /*
  ll ans2 = inf;
  vll fa2;

  afor(i,1,n-3)afor(j,i+1,n-2)afor(k,j+1,n-1)
  {
    vll v;
    v.pb(cum[i]);
    v.pb(cum[j]-cum[i]);
    v.pb(cum[k]-cum[j]);
    v.pb(cum[n]-cum[k]);
    sort(v.begin(),v.end());
    ll curr = v[3] - v[0];
    if(curr < ans2)
    {
      ans2 = curr;
      fa2 = {i,j,k};
    }
  }

  cout<<ans2<<endl;
  for(auto i:fa2)cout<<i<<" ";
    return 0;
    */



  
  ll ans = inf;
  afor(i,2,n-2)
  {
    vll v;

    int p1 = lower_bound(cum+1,cum+i,cum[i]/2) - cum;
    v.pb(cum[p1]);
    v.pb(cum[i] - cum[p1]);

    int p2 = lower_bound(cum+i+1,cum+n,cum[i] + (cum[n] - cum[i])/2) - cum;
    v.pb(cum[p2] - cum[i]);
    v.pb(cum[n] - cum[p2]);

    sort(v.begin(),v.end());
    ans = min(ans,v[3] - v[0]);
    v.clear();



    p1 = lower_bound(cum+2,cum+i+1,cum[i]/2) - cum - 1;
    v.pb(cum[p1]);
    v.pb(cum[i] - cum[p1]);

    p2 = lower_bound(cum+i+1,cum+n,cum[i] + (cum[n] - cum[i])/2) - cum;
    v.pb(cum[p2] - cum[i]);
    v.pb(cum[n] - cum[p2]);

    sort(v.begin(),v.end());
    ans = min(ans,v[3] - v[0]);
    v.clear();



    p1 = lower_bound(cum+1,cum+i,cum[i]/2) - cum;
    v.pb(cum[p1]);
    v.pb(cum[i] - cum[p1]);

    p2 = lower_bound(cum+i+2,cum+n+1,cum[i] + (cum[n] - cum[i])/2) - cum - 1;
    v.pb(cum[p2] - cum[i]);
    v.pb(cum[n] - cum[p2]);

    sort(v.begin(),v.end());
    ans = min(ans,v[3] - v[0]);
    v.clear();



    p1 = lower_bound(cum+2,cum+i+1,cum[i]/2) - cum - 1;
    v.pb(cum[p1]);
    v.pb(cum[i] - cum[p1]);

    p2 = lower_bound(cum+i+2,cum+n+1,cum[i] + (cum[n] - cum[i])/2) - cum - 1;
    v.pb(cum[p2] - cum[i]);
    v.pb(cum[n] - cum[p2]);

  

    sort(v.begin(),v.end());
    ans = min(ans,v[3] - v[0]);
    v.clear();



  }
  cout<<ans;
return 0;
}

Measured memory_bytes: 3328