#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

//template<class Type>
//Type solve(Type res = Type()){
//    int N; cin >> N;
//    cout << "ABC" + to_string(N) << endl;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    ll L,H; cin >> L >> H;
//    ll N; cin >> N;
//    vector<ll> A(N); for(auto& in:A) cin >> in;
//
//    for(auto a:A){
//        if(a < L){
//            cout << L-a << endl;
//        }else if(a > H){
//            cout << -1 << endl;
//        }else{
//            cout << 0 << endl;
//        }
//    }
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    int n,m; cin >> n >> m;
//
//    double theta1 =  (n%12 * 30) + 0.5 * m;
//    double theta2 = 6*m;
//
//
//    double diff = abs(theta1 - theta2);
//    if(diff > 90) diff -= 90;
//    cout << fixed << setprecision(12) << diff << endl;
//
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    vector<string> S(12);
//    for(auto& in:S) cin >> in;
//    ll sum = 0;
//
//    for(auto s:S){
//        sum += count(s.begin(),s.end(),'r')!=0;
//    }
//    cout << sum << endl;
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    int N; cin >> N;
//    function<void(int,string&)> rec = [&](int dep,string& str){
//        if(dep == N){
//            cout << str << endl;
//            return;
//        }
//
//        str.push_back('a');
//        rec(dep+1,str);
//        str.pop_back();
//
//        str.push_back('b');
//        rec(dep+1,str);
//        str.pop_back();
//
//        str.push_back('c');
//        rec(dep+1,str);
//        str.pop_back();
//
//    };
//    string str = "";
//    rec(0,str);
//
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    vector<ll> vs(5);
//    for(auto& in:vs) cin >> in;
//
//    vector<ll> x;
//    for(int i = 0; i < 5;i++){
//        for(int j = i+1; j < 5;j++){
//            for(int k = j+1; k < 5;k++){
//                x.push_back(vs[i]+vs[j]+vs[k]);
//            }
//        }
//    }
//    sort(x.rbegin(),x.rend());
//    cout << x[2] << endl;
//
//    return res;
//}

//template<class Type>
//Type solve(Type res = Type()){
//    ll ans = 0;
//    int N,M; cin >> N >> M;
//
//    vector<pll> edge(M);
//    for(auto& in:edge){
//        cin >> in.first >> in.second;
//    }
//    sort(edge.begin(),edge.end(),[](const pll& l,const pll& r){
//        return l.second < r.second;
//    });
//
//    ll left = 0;
//    for(int i = 0; i < M;i++){
//        if(edge[i].first >= left){
//            ans++;
//            left = edge[i].second;
//        }
//    }
//
//    cout << ans << endl;
//
//    return res;
//}

template<class Type>
Type solve(Type res = Type()){
    ll N; cin >> N;
    vector<ll> A(N); for(auto& in:A) cin >> in;
    
    vector<ll> cusum(N+1);
    for(int i = 0; i < N;i++){
        cusum[i+1] = cusum[i] + A[i];
    }
    
    
    ll ans = LINF;
    for(int m = 2; m < N-1; m++){
        ll PQ = cusum[m];
        ll RS = cusum[N] - cusum[m];
        
        ll it_l = lower_bound(cusum.begin(),cusum.end(),PQ/2) - cusum.begin();
        ll it_r = lower_bound(cusum.begin(),cusum.end(),PQ + RS/2) - cusum.begin();
        
        for(int dl = -20; dl <= 20; dl++){
            for(int dr = -20; dr <= 20; dr++){
                
                ll l_idx = it_l + dl;
                
                if(l_idx < 1) continue;
                if(l_idx >= m) continue;
                
                ll r_idx = it_r + dr;
                
                if(r_idx <= m) continue;
                if(r_idx >= N) continue;
                
                
                ll P = cusum[l_idx];
                ll Q = PQ - P;
                ll R = cusum[r_idx] - cusum[m];
                ll S = RS - R;
                
                ans = min(ans,max({P,Q,R,S})-min({P,Q,R,S}));
            }
        }
    }
    cout << ans << endl;
    return res;
}


int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve(0);
    // cout << fixed << setprecision(15) << solve<string>() << endl;
    return 0;
}


Measured memory_bytes: 3456