/**
 *    Author:  "Robin Singh" !!!!    
**/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define ff first
#define ss second
#define mp make_pair
#define str string 
#define szz(v) ((int)(v).size())  
#define fr(i, j, k) for(int i = j; i < k; i++)     // 0 -> n-1 (i,0,n)
#define bk(i, j, k) for(int i = j-1; i >= k; i--)  // n-1 -> 0 (i,n,0)
#define frit(it, a) for(__typeof((a).begin()) it = a.begin(); it != a.end(); ++it)
#define mem(ptr)  memset(ptr, 0, sizeof ptr)  // use fill() for value other than 0 //
#define rtn return 
#define itt(a)   __typeof((a).begin())
#define RT  (cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n")  // Run Time
#define nl "\n"  // New Line
#define dbg(x) (cerr << #x << " = " << x << "  ")
#define enl (cerr << "\n")
#define dbgg(x,y) (cerr << #x << " = " << x << " -> " << #y << " = " << y << "\n")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector< pair<int,int> > vpii;
typedef vector< pair<ll, ll> > vpll;
const long long mod = 1e9+7;
const ld eps = 1e-9;
const int inf = (1<<30);
const ll INF = (1LL<<61);

ll gcd(ll a,ll b) { return b==0 ? a : gcd(b, a%b); }
ll expo(ll x, ll y){ ll res = 1; x = x % mod;  while (y > 0) { if (y & 1)res = (res*x) % mod; y = y>>1;x = (x*x) % mod;}return res;}
void matmul(ll a[][2], ll b[][2]) { ll tmp[2][2]; for(int i=0; i<2; i++) { for(int j=0; j<2; j++) { tmp[i][j] = 0; for(int k=0; k<2; k++) tmp[i][j] = (tmp[i][j] + a[i][k]*b[k][j]) % mod; }} for(int i=0; i<2; i++) for(int j=0; j<2; j++) a[i][j] = tmp[i][j]; }
ll fibo(int n)    // returns nth fibonacci number, also S(n) = f(n+2)-1
{ if(n == 0) return 0; else if(n == 1) return 1; ll T[2][2] = { {0, 1} , {1, 1} }; ll res[2][2] = { {1, 0}, {0,1} }; n--; while(n) { if(n & 1) matmul(res, T); n >>= 1; matmul(T, T); } return res[1][1]; }

const int nn = 2e5 + 10;
ll a[nn], sum[nn], n;

pair <ll, ll> get(int frm, int to){
    pair<ll, ll> res;
    ll tmp = INF, l = frm, r = to;
    
    while(l <= r){                      // change the interval (using mid) as sum is monotonic
       // dbgg(frm, to);
        int mid = (l + r)/2;
        ll lsm = sum[mid] - sum[frm-1];
        ll rsm = sum[to] - sum[mid];
        
        if(abs(lsm-rsm) < tmp){
            tmp = abs(lsm-rsm);
            res = {lsm, rsm};
        }
        
        if(lsm <= rsm) l = mid+1;
        else r = mid-1;
    }
    return res;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    cin >> n;
    for(int i=1; i<=n; i++){
        cin >> a[i];
        sum[i] = sum[i-1] + a[i];
    }
    
    ll tmp, ans = INF;
    for(int i=2; i<=n-2; i++){                 // min two elements required in an interval
        pair<ll, ll> left = get(1, i);
        pair<ll, ll> right = get(i+1, n);
        ll mx = max({left.first, left.second, right.first, right.second});
        ll mn = min({left.first, left.second, right.first, right.second});
        tmp = mx-mn;
        ans = min(ans, tmp);
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 3328