//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;


int main(){
    long long a = 10;
    long long N;
    std::cin >> N;

    long long sum = 0;
    auto vec = vector<long long>(N);
    auto sum_vec = vector<long long>(N);
    auto Lidxs = vector<long long>(N);
    auto Ridxs = vector<long long>(N);

    FOR(i,0,N){
          std::cin >> vec[i] ;
          if(i == 0) sum_vec[i] = vec[i];
          else       sum_vec[i] = sum_vec[i-1] + vec[i];
    }
    FOR(i,1,N-2){
        if( i == 1 ) {
            Lidxs[1] = 0;
            Ridxs[N-3] = N-2;
        }else{
            long long sum_f = sum_vec[i] ;
            long long sum_s = sum_vec[N-1] - sum_f;

            long long s1 = sum_vec[Lidxs[i-1]];
            long long s2 = sum_f - s1;             
            long long s3 = sum_vec[Lidxs[i-1] + 1];
            long long s4 = sum_f - s3;             

            long long idx = Lidxs[i-1];
            
            while( abs(s1- s2) > abs(s3 - s4) ){
                idx++;

                s1 = sum_vec[idx];
                s2 = sum_f - s1;             
                s3 = sum_vec[idx + 1];
                s4 = sum_f - s3;             
            }
            Lidxs[i] = idx;
            
            sum_f = sum_vec[N-2-i] ;
            sum_s = sum_vec[N-1] - sum_f;
            
            s1 = sum_vec[Ridxs[N-1-i]] - sum_f;
            s2 = sum_s - s1;             
            s3 = sum_vec[Ridxs[N-1-i] - 1] - sum_f;
            s4 = sum_s - s3;             

            idx = Ridxs[N-1-i];
            
            while( abs(s1- s2) > abs(s3 - s4) ){
                idx--;

                s1 = sum_vec[idx] - sum_f;
                s2 = sum_s - s1;             
                s3 = sum_vec[idx - 1] - sum_f;
                s4 = sum_s - s3;             
            }
            Ridxs[N-2-i] = idx;
        }
    }

    long long ans = -1;
    FOR(i,1,N-2){
        long long s1 = sum_vec[Lidxs[i]];
        long long s2 = sum_vec[i] - sum_vec[Lidxs[i]];
        long long s3 = sum_vec[Ridxs[i]] - sum_vec[i];
        long long s4 = sum_vec[N-1] - sum_vec[Ridxs[i]];
        long long s_max = max(s1, max(s2, max(s3, s4)));
        long long s_min = min(s1, min(s2, min(s3, s4)));
        long long del = s_max - s_min;
        if(ans < 0 || del < ans ) ans = del;
    }

    cout << ans << endl;
    /* 
      for(auto itr = vec.begin(); itr != vec.end(); ++itr){
          std::cin >> *itr ;
          if 
      }
     */
    /* 
      int i1= 0;
      int sum_first = 0;
      FOR(i,0,N){
          if( abs(sum_first-(sum-sum_first)) >= abs(sum_first+vec[i]-(sum-sum_first-vec[i])) ){
              sum_first += vec[i];                
          }else{
              i1 = i;
              break;
          }
      }
      
      int sum_second = sum - sum_first;
      
      int asum1 = 0;
      FOR(i,0,i1){
          if( abs(asum1-(sum_first-asum1)) >= abs(asum1+vec[i]-(sum_first-asum1-vec[i])) ){
              asum1 += vec[i];                
          }else{
              break;
          }
      }
      int asum2 = sum_first - asum1;
      
      int asum3 = 0;
      FOR(i,i1,N){
          if( abs(asum3-(sum_second-asum3)) >= abs(asum3+vec[i]-(sum_second-asum3-vec[i])) ){
              asum3 += vec[i];                
          }else{
              break;
          }
      }
      
      int asum4 = sum_second - asum3;
      
      int max_s = max( asum1 , max( asum2 , max( asum3 , asum4)));
      int min_s = min( asum1 , min( asum2 , min( asum3 , asum4)));
      cout << abs(max_s - min_s) << endl;
     */
    
}






Measured memory_bytes: 6528