#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
typedef pair<int, double> Pid;
typedef pair<double, int> Pdi;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<int, pair<int, int>> PP;
typedef pair<P, int> PPi;
constexpr double PI = 3.1415926535897932;   // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1e+9 + 7;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define chadd(x, y) x = (x + y) % mod

template<typename Monoid>
struct SegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;

    int sz;
    vector<Monoid> seg;

    const F f;      // モノイドに対して二項演算を行う関数オブジェクト
    const Monoid M1;

    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1){
        sz = 1;
        while(sz < n)   sz <<= 1;
        seg.assign(2 * sz, M1);
    }

    void set(int k, const Monoid &x){
        seg[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    void update(int k, const Monoid &x){
        k += sz;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k << 1], seg[k << 1 | 1]);
        }
    }

    Monoid query(int a, int b){
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1){
            if(a & 1)   L = f(L, seg[a++]);
            if(b & 1)   R = f(seg[--b], R);
        }
        return f(L, R);
    }

    Monoid operator[](const int &k) const{
        return seg[k + sz];
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    SegmentTree<ll> seg(n, [](ll a, ll b){return a + b;}, 0ll);
    for(int i = 0; i < n; ++i){
        ll a;
        cin >> a;
        seg.set(i, a);
    }
    seg.build();
    ll ans = 1e+17;
    for(int i = 2; i + 1 < n; ++i){
        ll leftsum = seg.query(0, i), rightsum = seg.query(i, n);
        leftsum >>= 1, rightsum >>= 1;
        ll maxe = 0, mine = 1e+17;
        int lb = 1, ub = i;
        while(ub - lb > 1){
            int mid = (lb + ub) >> 1;
            if(seg.query(0, mid) < leftsum)    lb = mid;
            else    ub = mid;
        }
        ll left = seg.query(0, lb), right = seg.query(lb, i);
        chmax(maxe, max(left, right));
        chmin(mine, min(left, right));
        if(lb + 1 > 0 && lb + 1 < i){
            left = seg.query(0, lb + 1), right = seg.query(lb + 1, i);
            if(abs(left - right) < maxe - mine){
                maxe = max(left, right);
                mine = min(left, right);
            }
        }

        lb = i + 1, ub = n;
        while(ub - lb > 1){
            int mid = (lb + ub) >> 1;
            if(seg.query(mid, n) >= rightsum)   lb = mid;
            else    ub = mid;
        }
        left = seg.query(i, lb), right = seg.query(lb, n);
        ll maxe2 = 0, mine2 = 1e+17;
        chmax(maxe2, max(left, right));
        chmin(mine2, min(left, right));
        if(lb + 1 > i && lb + 1 < n){
            left = seg.query(i, lb + 1), right = seg.query(lb + 1, n);
            if(abs(left - right) < maxe2 - mine2){
                maxe2 = max(left, right);
                mine2 = min(left, right);
            }
        }

        chmin(ans, max(maxe, maxe2) - min(mine, mine2));
    }
    cout << ans << endl;
}

Measured memory_bytes: 4352