#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <climits>
#include <map>
#include <unordered_map>
#include <string.h>
#include<iomanip>
#include <iterator>
#include <locale>

#define all(a) a.begin(),a.end()
#define VV(T) vector<vector< T > >
#define rep(i,N) for(int i = 0; i <N; i++)
#define repsg(i,s,g) for(int i = s; i <g; i++)
#define name(x) #x
using namespace std;

const int INF = INT_MAX;
const long long INFL = 1LL<<40;
const double PI = atan(1)*4;
//const int MOD = pow(10,9)+7;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

typedef long long ll,LL;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
typedef pair<int,int> pii;
typedef pair<LL, LL> pLL;


// vector入力
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
    for(T& x: vec) is >> x;
    return is;
}
// vector出力
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec){
    for(int i=0; i<vec.size(); i++){
        os << vec[i] << ( i+1 == vec.size() ? "" : " " );
    }
    return os;
}



int main() {
    int n;cin>>n;
    
    vector<ll> a(n);cin>>a;
    
    vector<ll> cum(n);
    cum[0] = a[0];
    repsg(j,1,n){
        cum[j] = cum[j-1]+a[j];
    }
    
    ll diff = INFL;
    
    ll b,c,d,e,mx,mn;
    
    repsg(i,2,n-1){
        
        /// a[0]....a[i-1], a[i]...a[n-1]に分解
        
        auto bc = lower_bound(all(cum),(double)cum[i-1]/2.0)-cum.begin();
        auto de = lower_bound(all(cum),(double)(cum[n-1]+cum[i-1])/2.0)-cum.begin();
        b = cum[bc];
        c = cum[i-1]-b;
        d = cum[de]-cum[i-1];
        e = cum[n-1] - cum[de];
        
        mx = b;
        mx = max(mx,c);mx=max(mx,d);mx=max(mx,e);
        
        mn = b;
        mn = min(mn,c);mn=min(mn,d);mn=min(mn,e);
        
        diff = min(diff,mx-mn);
        
        bc--;
        if(bc>=0 && de>=0){
            b = cum[bc];
            c = cum[i-1]-b;
            d = cum[de]-cum[i-1];
            e = cum[n-1] - cum[de];
        
            mx = b;
            mx = max(mx,c);mx=max(mx,d);mx=max(mx,e);
        
            mn = b;
            mn = min(mn,c);mn=min(mn,d);mn=min(mn,e);
        
            diff = min(diff,mx-mn);
        }
        
        bc++;de--;
        if(bc>=0 && de>=0){
            b = cum[bc];
            c = cum[i-1]-b;
            d = cum[de]-cum[i-1];
            e = cum[n-1] - cum[de];
            
            mx = b;
            mx = max(mx,c);mx=max(mx,d);mx=max(mx,e);
            
            mn = b;
            mn = min(mn,c);mn=min(mn,d);mn=min(mn,e);
            
            diff = min(diff,mx-mn);
        }
        

        bc--;
        if(bc>=0 && de>=0){
            b = cum[bc];
            c = cum[i-1]-b;
            d = cum[de]-cum[i-1];
            e = cum[n-1] - cum[de];
            
            mx = b;
            mx = max(mx,c);mx=max(mx,d);mx=max(mx,e);
            
            mn = b;
            mn = min(mn,c);mn=min(mn,d);mn=min(mn,e);
            
            diff = min(diff,mx-mn);
        }
        
    }
    
    
    cout<<diff<<endl;
    
    return 0;
}



Measured memory_bytes: 3328