#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+10;
ll a[maxn], n, sum[maxn], mi;
ll f(ll p1,ll p2,ll i){
    ll mi = min(sum[p1], sum[i]-sum[p1]);
    ll ma = max(sum[p1], sum[i]-sum[p1]);
    mi = min(mi, min(sum[p2]-sum[i], sum[n]-sum[p2]));
    ma = max(ma, max(sum[p2]-sum[i], sum[n]-sum[p2]));
    return ma-mi;
}
int main(){
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
        sum[i] = sum[i-1]+a[i];
    }
    mi = sum[n];
    for(ll i=2;i<=n-2;i++){
        //cout<<sum[i]/2<<endl;
        ll p1 = lower_bound(sum, sum+n+1, sum[i]/2)-sum-1;
        ll p2 = lower_bound(sum, sum+n+1, sum[i]+(sum[n]-sum[i])/2)-sum-1;
        //cout<<p1<<" == "<<p2<<endl;
        mi = min(mi, f(p1, p2, i));
        if(p1 != i-1) mi = min(mi, f(p1+1, p2, i));
        if(p2 != n-1) mi = min(mi, f(p1, p2+1, i));
        if(p1 != i-1 && p2 != n-1) mi = min(mi, f(p1+1, p2+1, i));
    }
    cout<<mi<<endl;
    return 0;
}
