#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
ll n;
ll a[200005];
vector<ll> v;
pair<ll,ll> lft[200005];
pair<ll,ll> rgt[200005];
int main()
{
        cin >> n;
        for(ll i=1;i<=n;i++) {
                cin >> a[i];
        }
        ll sums = a[1];
        v.pb(2*a[1]);
        for(ll i=2;i<=n;i++) {
                sums += a[i];
                v.pb(2*sums);
                auto it = lower_bound(v.begin() , v.end() , sums);
                if(*it == sums) {
                        lft[i] = {sums/2 , sums/2};
                }else {
                        /*it++;
                        auto nxt = it;
                        it--;
                        it--;
                        ll el1 = *nxt/2;
                        ll el2 = *it/2;
                        ll val1 = abs(el1 + el1 - sums);
                        ll val2 = abs(el2 + el2 - sums);
                        if(val1 < val2) {
                                lft[i] = {el1 , sums - el1};
                        }else {
                                lft[i] = {el2 , sums - el2};
                        }*/
                        if(it == v.begin()) {

                                ll el = *it/2;
                                lft[i] = {el , sums - el}; //continue;
                        }else {
                        auto nxt = it;
                        it--;
                        //it--;
                        ll el1 = *nxt/2;
                        ll el2 = *it/2;
                        ll val1 = abs(el1 + el1 - sums);
                        ll val2 = abs(el2 + el2 - sums);
                        if(val1 < val2) {
                                lft[i] = {el1 , sums - el1};
                        }else {
                                lft[i] = {el2 , sums - el2};
                        }
                        }
                }
                //cout << i << "--->" << lft[i].first << " " << lft[i].second << endl;
        }
        //return 0;
        v.clear();
        sums = a[n];
        v.pb(sums + sums);
        for(ll i=n-1; i>=1;i--) {

                sums += a[i];
                v.pb(sums + sums);
                auto it = lower_bound(v.begin() , v.end() , sums);
                if(*it == sums) {
                        rgt[i] = {sums/2 , sums/2};
                }else {
                        /*it++;
                        auto nxt = it;
                        it--;
                        it--;
                        ll el1 = *nxt/2;
                        ll el2 = *it/2;
                        ll val1 = abs(el1 + el1 - sums);
                        ll val2 = abs(el2 + el2 - sums);
                        if(val1 < val2) {
                                rgt[i] = {el1 , sums - el1};
                        }else {
                                rgt[i] = {el2 , sums - el2};
                        }*/
                        if(it == v.begin()) {

                                ll el = *it/2;
                                rgt[i] = {el , sums - el}; //continue;
                        }else {
                        auto nxt = it;
                        it--;
                        //it--;
                        ll el1 = *nxt/2;
                        ll el2 = *it/2;
                        ll val1 = abs(el1 + el1 - sums);
                        ll val2 = abs(el2 + el2 - sums);
                        if(val1 < val2) {
                                rgt[i] = {el1 , sums - el1};
                        }else {
                                rgt[i] = {el2 , sums - el2};
                        }
                        }
                }

        }
        ll ans = 1e16;
        for(ll i=2;i<=n-2;i++) {
                vector<ll> X;
                X.pb(lft[i].first);
                X.pb(lft[i].second);
                X.pb(rgt[i+1].first);
                X.pb(rgt[i+1].second);
                sort(X.begin() , X.end());
                ans = min(ans , X[3] - X[0]);
        }
        cout << ans << endl;
        return 0;
}


Measured memory_bytes: 9712