// NOT ENDED
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll SIZ = ((ll)7e5 + 5), LONGBOI = ((ll)1e17 + 5);
mt19937_64 REALrandom(chrono::system_clock::to_time_t(chrono::system_clock::now()));

struct mycompare{
    bool operator()(ll a, ll b) const{
        return a%10 > b%10;
    }
};

struct str{
    ll n;
};

bool operator < (str a, str b){
    return a.n < b.n;
}

ll n;
ll a[SIZ],p[SIZ];

ll Check(ll &s1, ll &s2, ll &s3){
    cout << "CHECK : s1 = " << s1 << " s2 = " << s2 << " s3 = " << s3 << " and ans = ";
    if(s3<=0||s2<=0||s1<=0||s1+s2+s3>n) {
        cout<<LONGBOI<<endl;
        return LONGBOI;
    }

    ll pos=1;
    ll s[5];s[1]=s1;s[2]=s2;s[3]=s3;
    ll sum[5];sum[1]=0;sum[2]=0;sum[3]=0;

    for(int i=1;i<=3;i++){
        sum[i]+=p[pos+s[i]-1]-p[pos-1];
        pos+=s[i];
    }
    if(pos < n){
        sum[1]+=p[n]-p[pos-1];
    }
    ll mx = max(sum[1], max(sum[2], sum[3]));
    ll mn = min(sum[1], min(sum[2], sum[3]));
    cout << mx-mn << " because : sum1 = " << sum[1] << " sum2 = " << sum[2] << " sum3 = " << sum[3] << endl;
    return mx-mn;
}

pll Search3(ll &s1, ll &s2){
    ll l=1, r=n-(s1+s2)+1;
    ll tml, tmr, al, ar;
    while(l+1<r){
        cout << "Search3 : l = " << l << " r = " << r << endl;
        tml = l + (r-l+1)/3; al = Check(s1, s2, tml);
        tmr = r - (r-l+1)/3; ar = Check(s1, s2, tmr);
        if(al < ar || (al == ar && al == LONGBOI)) r = tmr;
        else l = tml;
    }
    cout << "Search3 ended : s1 = " << s1 << " s2 = " << s2 << " s3 = " << l << endl;
    al = Check(s1, s2, l);
    return {al, l};
}

pll Search2(ll &s1){
    ll l=1, r=n-s1+1;
    ll tml, tmr, al, ar;
    while(l+1<r){
        cout << "Search2 : l = " << l << " r = " << r << endl;
        tml = l + (r-l+1)/3; al = Search3(s1, tml).first;
        tmr = r - (r-l+1)/3; ar = Search3(s1, tmr).first;
        if(al < ar || (al == ar && al == LONGBOI)) r = tmr;
        else l = tml;
    }
    cout << "Search2 ended : s1 = " << s1 << " s2 = " << l << endl;
    al = Search3(s1, l).first;
    return {al, l};
}

pll Search1(){
    ll l=1, r=n+1;
    ll tml, tmr, al, ar;
    while(l+1<r){
        cout << "Search1  : l = " << l << " r = " << r << endl;
        tml = l + (r-l+1)/3; al = Search2(tml).first;
        tmr = r - (r-l+1)/3; ar = Search2(tmr).first;
        if(al < ar || (al == ar && al == LONGBOI)) r = tmr;
        else l = tml;
    }
    cout << "Search1 ended : s1 = " << l << endl;
    al = Search2(l).first;
    return {al, l};
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    freopen("output.txt", "w", stdout);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        p[i]=p[i-1]+a[i];
    }
    ll ans=LONGBOI;
    pll res = Search1();
    cout << "ANS = " << res.first << endl;
    return 0;
}
