/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip>
#include <limits>//setprecision
//#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
//#include <unordered_set> //unordered_set
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#define endl "\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const int MOD = 1000000007;
typedef pair<long long,long long> P;

void chmax(long long &a,long long b){if(a < b) a = b;}
void chmin(long long &a,long long b){if(a > b) a = b;}

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % MOD, M / 2);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % MOD;
    return res %= MOD;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}



int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N;cin >> N;
    vector<long long> retu(N);
    for(int i = 0;i<N;i++) cin >> retu.at(i);
    vector<long long> rui(N+1);
    for(int i = 0;i<N;i++){
        rui.at(i+1) = rui.at(i)+retu.at(i);
    }
    //中央切りを全探索
    long long ans = INF;
    for(int i = 0;i<=N;i++){
        //左側
        int left = 0,right = i;
        while(right - left > 1){
            int mid = (left+right)/2;
            if(rui.at(mid) < rui.at(i)-rui.at(mid)){
                left = mid;
            }
            else{
                right = mid;
            }
        }
        long long P = rui.at(left);long long Q = rui.at(i)-rui.at(left);
        //一応left,rightの両方見ておく
        long long P2 = rui.at(right);long long Q2 = rui.at(i)-rui.at(right);
        long long LEFT_MAX = -INF,LEFT_MIN = INF;
        if(abs(P-Q) > abs(P2-Q2)){
            LEFT_MAX = max(P2,Q2);
            LEFT_MIN = min(P2,Q2);
        }
        else{
            LEFT_MAX = max(P,Q);
            LEFT_MIN = min(P,Q);
        }
        //右側
        left = i,right = N;
        while(right - left > 1){
            int mid = (left+right)/2;
            if(rui.at(mid)-rui.at(i) < rui.at(N)-rui.at(mid)){
                left = mid;
            }
            else{
                right = mid;
            }
        }
        long long R = rui.at(left)-rui.at(i);long long S = rui.at(N)-rui.at(left);
        long long R2 = rui.at(right)-rui.at(i);long long S2 = rui.at(N)-rui.at(right);
        long long RIGHT_MAX = -INF,RIGHT_MIN = INF;
        if(abs(R-S) > abs(R2-S2)){
            RIGHT_MAX = max(R2,S2);
            RIGHT_MIN = min(R2,S2);
        }
        else{
            RIGHT_MAX = max(R,S);
            RIGHT_MIN = min(R,S);
        }
        ans = min(ans,max(LEFT_MAX,RIGHT_MAX) - min(LEFT_MIN,RIGHT_MIN));
    }
    cout << ans << endl;
}

Measured memory_bytes: 6368