/*
memo for solution
*/
#include <cstdlib> 
#include <numeric>
#include <cstring>
#include <cstdio> 
#include <cfloat> 
#include <map> 
#include <cassert>
#include <cmath> 
#include <climits> 
#include <utility> 
#include <set> 
#include <iostream> 
#include <memory> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <functional> 
#include <sstream> 
#include <complex> 
#include <stack> 
#include <queue> 
#include <iomanip>
#include<sys/time.h>
using namespace std; 
double e_time(void){static struct timeval now;gettimeofday(&now, NULL);return (double)(now.tv_sec  + now.tv_usec/1000000.0);}
#define REP(i,b,n) for(int i=b;i<n;i++) 
#define rep(i,n)      REP(i,0,n) 
#define pb push_back  
#define mp make_pair 
#define ALL(C)   (C).begin(),(C).end() 
#define fe(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr != (c).end();itr++)
#define BITSHIFT(X)     ( (1<<(X)) )
#define CONTAIN(S,X) ( ((S)&BITSHIFT(X)) != 0)
typedef complex<double>P; 
typedef long long ll; 
typedef unsigned long long ull; 
typedef pair<int,int> pii; 
typedef vector<int> vint;
template<class T> void vp(T &a,int p){rep(i,p)cout << a[i]<<" ";cout << endl;}  
template<class T> T ceilUp(const T& a,const T& b){return (a+b-1)/b;}
const ll mod =   1000000009;
inline ll modadd(ll a,ll b){a %= mod;b %= mod;return (a+b)%mod;}
inline ll modsub(ll a,ll b){a %= mod;b %= mod;a-=b;a%=mod;if (a<0)a+=mod;return a;}
inline ll modmul(ll a,ll b){a %= mod;b %= mod;return (a*b)%mod;}
inline ll modeq(ll a,ll b,ll m){a = (a%m+m)%m; b = (b%m+m)%m;return a == b;}
//ll modpow(ll n,ll p){if (p == 0)return 1;if (p == 1)return n;ll ret=mypow(n,p/2);ret=ret*ret%mod;if (p&1)ret=ret*n%mod;return ret;}
//ll getinv(ll a){return mypow(a,mod-2);}


ll compute(ll a, ll b, ll c , ll d){
  vector<ll> t = {a, b, c, d};
  ll maxi = 0;
  for(int i = 0;i < 4;i++){
    for(int j = 1;j < 4;j++){
      maxi = max(maxi, std::abs(t[i] - t[j]));
    }
  }
  return maxi;
}

void bf(int n, vector<ll> & a){
  ll sum = LONG_LONG_MAX;
  int minii=0, minij=1, minik=2, minil=3;
  for(int i = 0;i < n;i++){
    for(int j = i+1;j < n;j++){
      for(int k = j+1;k+1 < n;k++){
	ll tmpa = accumulate(begin(a), begin(a)+i+1, 0);
	ll tmpb = accumulate(begin(a)+i+1, begin(a)+j+1, 0); 
	ll tmpc = accumulate(begin(a)+j+1, begin(a)+k+1, 0); 
	ll tmpd = accumulate(begin(a)+k+1, end(a), 0);
	ll tmp = compute(tmpa, tmpb, tmpc, tmpd);
	if (tmp < sum){
	  sum = tmp;
	  minii = i;
	  minij = j;
	  minik = k;
	}
      }
    }
  }
  cout << "[0," << minii << "]" << endl;
  cout << "[" << minii+1 << "," << minij << "]" << endl;
  cout << "[" << minij+1 << "," << minik << "]" << endl;
  cout << "[" << minik+1 << "," << n-1 << "]" << endl;
  cout << accumulate(begin(a), begin(a)+minii+1, 0) << " " <<  
    accumulate(begin(a)+minii+1, begin(a)+minij+1, 0) <<" " <<  
    accumulate(begin(a)+minij+1, begin(a)+minik+1, 0)<< " " <<  
    accumulate(begin(a)+minik+1, end(a), 0) << endl;
  cout << sum << endl;
}


// [beg, end)
pair<ll, ll> getPair(const int beg, const int end, const vector<ll> & sum){
  ll base = 0;
  if (beg != 0){
    base = sum[beg-1];
  }
  int left = beg, right = end-2;
  while(left < right){
    int mid = (left + right) / 2;
    ll lsum = sum[mid] - base;
    ll rsum = sum[end-1] - sum[mid];
    if (lsum < rsum){// right is too big
      left = mid+1;
    } else if (lsum >= rsum){// right is too small
      right = mid;
    }
  }
  // [beg, left), [left, end)
  ll lsum = sum[left] - base, rsum = sum[end-1] - sum[left];
  ll diff = std::abs(lsum - rsum);
  assert(lsum != 0);
  //cout << "getPair " << beg <<" " << left <<" " << end <<" " << lsum <<" " << rsum << endl;
  for(int i = left-2;i < left+2&& i < end-1;i++){
    if (i < beg)continue;
    ll tlsum = sum[i]-base, trsum = sum[end-1] - sum[i];
    ll tdiff = std::abs(tlsum - trsum);
    //cout <<"hoge " << i <<" " << tlsum <<" " << trsum << " " << end << endl;
    if (tdiff < diff){
      lsum = tlsum;
      rsum = trsum;
      diff = tdiff;
    }
  }
  return make_pair(max(lsum, rsum), min(lsum, rsum));
}

ll solve(const int n, vector<ll> & a){
  ll ret = LONG_LONG_MAX;
  vector<ll> sum(n);
  sum[0] = a[0];
  for(int i = 1;i < n;i++){
    sum[i] = sum[i-1] + a[i];
  }

  for(int i = 1;i < n-2;i++){
    /*
    if (i == 3){
      cout <<"---" << endl;
    }
    */
    pair<ll, ll> tmp = getPair(0, i+1, sum);// .first > .second
    pair<ll, ll> tmp2 = getPair(i+1, n, sum);// .first > .second
    /*
    if (i == 3){
      cout <<"i = " << i << endl;
      cout << 0 << " " << i <<" " << i+1 << " " << n << endl;
      cout << tmp.first <<" " << tmp.second << endl;
      cout << tmp2.first <<" " << tmp2.second << endl;
      cout <<"---" << endl;
    }
    */
    ret = min(ret, max(tmp.first, tmp2.first) - min(tmp.second, tmp2.second));
  }
  return ret;
}

int main(){
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n){
    cin >> a[i];
  }
  cout << solve(n, a) << endl;
  return 0;
}
 
 


Measured memory_bytes: 3328