#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef complex<double> xy_t;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
ll inf = llmax / 2;
double eps = 1e-11;

int main() {
    int N;
    cin >> N;
    ll A[N];
    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        if (i == 0) {
            A[0] = a;
        } else {
            A[i] = A[i - 1] + a;
        }
    }
    ll mindiff[N];
    fill(mindiff, mindiff + N, llmax);
    ll diff = llmax;
    for (int i = 1; i < N - 2; i++) {
        int min = -1;
        int max = i;
        ll presum = A[i];
        while (max - min > 1) {
            int now = (min + max) / 2;
            if (A[now] >= (presum + 1) / 2) {
                max = now;
            } else {
                min = now;
            }
        }
        ll pre[2][2];
        pre[0][0] = (max < i) ? A[max] : llmax;
        pre[0][1] = (max < i) ? presum - A[max] : 0;
        pre[1][0] = (max > 0) ? A[max - 1] : llmax;
        pre[1][1] = (max > 0) ? presum - A[max - 1] : 0;
        min = i;
        max = N - 1;
        ll latesum = A[N - 1] - A[i];
        while (max - min > 1) {
            int now = (min + max) / 2;
            if (A[now] - A[i] >= (latesum + 1) / 2) {
                max = now;
            } else {
                min = now;
            }
        }
        ll late[2][2];
        late[0][0] = (max < N - 1) ? A[max] - A[i] : llmax;
        late[0][1] = (max < N - 1) ? latesum - late[0][0] : 0;
        late[1][0] = (max > i + 1) ? A[max - 1] - A[i] : llmax;
        late[1][1] = (max > i + 1) ? latesum - late[1][0] : 0;
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                ll mmax = llmin;
                ll mmin = llmax;
                for (int l = 0; l < 2; l++) {
                    if (pre[j][l] > mmax) {
                        mmax = pre[j][l];
                    }
                    if (pre[j][l] < mmin) {
                        mmin = pre[j][l];
                    }
                    if (late[k][l] > mmax) {
                        mmax = late[k][l];
                    }
                    if (late[k][l] < mmin) {
                        mmin = late[k][l];
                    }
                }
                ll mdiff = mmax - mmin;
                //cout << mdiff << " " << mmax << " " << mmin << endl;
                if (diff > mdiff) {
                    diff = mdiff;
                }
                if (mindiff[i] > mdiff) {
                    mindiff[i] = mdiff;
                }
            }
        }
        //cout << "bbb" << endl;
    }
    // cout << "aaa" << endl;
    // for (int i = 1; i < N - 2; i++) {
    //     cout << mindiff[i] << endl;
    // }
    // cout << "aaa" << endl;
    cout << diff << endl;
}

Measured memory_bytes: 3328