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

int main(){

    long long N;
    cin >> N;
    vector<long long> A(N);
    cin >> A[0];
    for(long long i = 1; i < N; i++){
        cin >> A[i];
        A[i] += A[i - 1];
    }

    long long min = 1e12;
    for(long long middle = 1; middle < N - 1; middle++){
        
        vector<long long> sec(4);
        
        long long half_left = A[middle] / 2;
        auto left_itr = lower_bound(A.begin(), A.begin() + middle, half_left);
        long long left = distance(A.begin(), left_itr);
        long long half_right = A[middle] + (A[N - 1] - A[middle]) / 2;
        auto right_itr = lower_bound(A.begin() + middle + 1, A.begin() + N - 2, half_right);
        long long right = distance(A.begin(), right_itr);

        sec[0] = A[left];
        sec[1] = A[middle] - A[left];
        sec[2] = A[right] - A[middle];
        sec[3] = A[N - 1] - A[right];
        sort(sec.begin(), sec.end());
        if(min > sec.back() - sec.front()) min = sec.back() - sec.front();

        if(left - 1 >= 0){
            sec[0] = A[left - 1];
            sec[1] = A[middle] - A[left - 1];
            sec[2] = A[right] - A[middle];
            sec[3] = A[N - 1] - A[right];
            sort(sec.begin(), sec.end());
            if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            if(right - 1 > middle){
                sec[0] = A[left - 1];
                sec[1] = A[middle] - A[left - 1];
                sec[2] = A[right - 1] - A[middle];
                sec[3] = A[N - 1] - A[right - 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
            if(right + 1 < N - 1){
                sec[0] = A[left - 1];
                sec[1] = A[middle] - A[left - 1];
                sec[2] = A[right + 1] - A[middle];
                sec[3] = A[N - 1] - A[right + 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
        }

        if(left + 1 < middle){
            sec[0] = A[left + 1];
            sec[1] = A[middle] - A[left + 1];
            sec[2] = A[right] - A[middle];
            sec[3] = A[N - 1] - A[right];
            sort(sec.begin(), sec.end());
            if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            if(right - 1 > middle){
                sec[0] = A[left + 1];
                sec[1] = A[middle] - A[left + 1];
                sec[2] = A[right - 1] - A[middle];
                sec[3] = A[N - 1] - A[right - 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
            if(right + 1 < N - 1){
                sec[0] = A[left + 1];
                sec[1] = A[middle] - A[left + 1];
                sec[2] = A[right + 1] - A[middle];
                sec[3] = A[N - 1] - A[right + 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
        }

        if(right - 1 > middle){
            sec[0] = A[left];
            sec[1] = A[middle] - A[left];
            sec[2] = A[right - 1] - A[middle];
            sec[3] = A[N - 1] - A[right - 1];
            sort(sec.begin(), sec.end());
            if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            if(left - 1 >= 0){
                sec[0] = A[left - 1];
                sec[1] = A[middle] - A[left - 1];
                sec[2] = A[right - 1] - A[middle];
                sec[3] = A[N - 1] - A[right - 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
            if(left + 1 < middle){
                sec[0] = A[left + 1];
                sec[1] = A[middle] - A[left + 1];
                sec[2] = A[right - 1] - A[middle];
                sec[3] = A[N - 1] - A[right - 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
        }

        if(right + 1 < N - 1){
            sec[0] = A[left];
            sec[1] = A[middle] - A[left];
            sec[2] = A[right + 1] - A[middle];
            sec[3] = A[N - 1] - A[right + 1];
            sort(sec.begin(), sec.end());
            if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            if(left - 1 >= 0){
                sec[0] = A[left - 1];
                sec[1] = A[middle] - A[left - 1];
                sec[2] = A[right + 1] - A[middle];
                sec[3] = A[N - 1] - A[right + 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
            if(left + 1 < middle){
                sec[0] = A[left + 1];
                sec[1] = A[middle] - A[left + 1];
                sec[2] = A[right + 1] - A[middle];
                sec[3] = A[N - 1] - A[right + 1];
                sort(sec.begin(), sec.end());
                if(min > sec.back() - sec.front()) min = sec.back() - sec.front();
            }
        }
    }
    
    cout << min << endl;

    return 0;
}

Measured memory_bytes: 1792