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

long long int N, ans;
vector<long long> A;

int main(){
    scanf("%lld", &N);
    for(int i = 0; i < N; ++i){
        scanf("%lld", &ans);
        A.push_back(ans);
    }
    partial_sum(A.begin(), A.end(), A.begin());
    ans = 300000000000000;
    auto it = A.begin() + 1, ed = A.end() - 1, a = A.begin(), b = it + 1;
    for(; it + 1 != ed; ++it){
        while(a + 1 != it){
            if(*a < *it - * (a + 1)){
                ++a;
            }else{
                break;
            }
        }
        if(b == it)++b;
        while(b + 1 != ed){
            if(*b - *it < *ed - * (b + 1)){
                ++b;
            }else{
                break;
            }
        }
        ans = min<long long>(ans, max<long long>({*a, *it - *a, *b - *it, A[N - 1] - *b}) - min<long long>({*a, *it - *a, *b - *it, A[N - 1] - *b}));
    }
    cout << ans << endl;
    return 0;
}