#include<bits/stdc++.h>
using namespace std;

vector< pair<int,int> > helper(vector<int> &a, int n) {
    vector< pair<int,int> > ans(n);
    ans[1] = {a[0], a[1]};
    int fE = 0; //firstEnding -> the index at which the first part is ending
    
    for(int i=2; i<n; i++) {
        ans[i] = ans[i-1];
        //include the current element in the right part
        ans[i].second += a[i];
        
        int cur = abs(ans[i].first - ans[i].second);
        //expand the first part if it is profitable
        while(fE+1 < i && abs(ans[i].first+a[fE+1]-(ans[i].second-a[fE+1])) < cur) {
            fE++;
            ans[i].first += a[fE];
            ans[i].second -= a[fE];
            cur = abs(ans[i].first - ans[i].second);
        }
    }
    
    return ans;
}

int main() {
    int n;
    cin>>n;
    vector<int> a(n);
    for(int &x : a) cin>>x;
    
    vector< pair<int,int> > pref, suf;
    pref = helper(a, n);
    reverse(a.begin(), a.end());
    suf = helper(a, n);
    reverse(suf.begin(), suf.end());
    
    int ans = INT_MAX;
    for(int i=1; i<n-2; i++) {
        vector<int> temp{pref[i].first, pref[i].second, suf[i+1].first, suf[i+1].second};
        sort(temp.begin(), temp.end());
        ans = min(ans, abs(temp.front() - temp.back()));
    }
    
    cout<<ans<<"\n";
}