#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

int main() {
    //cout.precision(10);
    int n;
    cin >> n;
    long long a[220000];
    long long sum[220000];
    sum[0] = 0;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
    }
    long long ans = 100000000000000000;
    //cout << ans << endl;
    for(int i = 3; i <= n - 1; i++){
        //cout << endl;
        //cout << i << endl;
        int d = i;
        int b = 1;
        int ok = 1;
        int ng = i - 1;
        while(ng - ok > 1){
            int mid = (ok + ng) / 2;
            if(sum[mid] * 2 < sum[i - 1]){
                ok = mid;
            } else {
                ng = mid;
            }
        }
        int c = ok + 1;
        ok = i;
        ng = n;
        while(ng - ok > 1){
            int mid = (ok + ng) / 2;
            if((sum[mid] - sum[i - 1]) * 2 < (sum[n] - sum[i - 1])){
                ok = mid;
            } else {
                ng = mid;
            }
        }
        long long p, q, r, s;
        int e = ok + 1;
        p = sum[c - 1];
        q = sum[d - 1] - sum[c - 1];
        r = sum[e - 1] - sum[d - 1];
        s = sum[n] - sum[e - 1];
        //cout << ans << endl;
        if((0 < b) && (b < c) && (c < d) && (d < e) && (e <= n)){
            //cout << b << " " << c << " " << d << " " << e << endl;
            //cout << p << " " << q << " " << r << " " << s << endl;
            //cout << max(p, max(q, max(r, s))) - min(p, min(q, min(r, s))) << endl;
            long long checker = max(p, max(q, max(r, s))) - min(p, min(q, min(r, s)));
            ans = min(ans, checker);
            //cout << ans << endl;
        }
        c++;
        p = sum[c - 1];
        q = sum[d - 1] - sum[c - 1];
        r = sum[e - 1] - sum[d - 1];
        s = sum[n] - sum[e - 1];
        if((0 < b) && (b < c) && (c < d) && (d < e) && (e <= n)){
            //cout << b << " " << c << " " << d << " " << e << endl;
            //cout << p << " " << q << " " << r << " " << s << endl;
            //cout << max(p, max(q, max(r, s))) - min(p, min(q, min(r, s))) << endl;
            long long checker = max(p, max(q, max(r, s))) - min(p, min(q, min(r, s)));
            ans = min(ans, checker);
            //cout << ans << endl;
        }
        c--;
        e++;
        p = sum[c - 1];
        q = sum[d - 1] - sum[c - 1];
        r = sum[e - 1] - sum[d - 1];
        s = sum[n] - sum[e - 1];
        if((0 < b) && (b < c) && (c < d) && (d < e) && (e <= n)){
            //cout << b << " " << c << " " << d << " " << e << endl;
            //cout << p << " " << q << " " << r << " " << s << endl;
            //cout << max(p, max(q, max(r, s))) - min(p, min(q, min(r, s))) << endl;
            long long checker = max(p, max(q, max(r, s))) - min(p, min(q, min(r, s)));
            ans = min(ans, checker);
            //cout << ans << endl;
        }
        c++;
        p = sum[c - 1];
        q = sum[d - 1] - sum[c - 1];
        r = sum[e - 1] - sum[d - 1];
        s = sum[n] - sum[e - 1];
        if((0 < b) && (b < c) && (c < d) && (d < e) && (e <= n)){
            //cout << b << " " << c << " " << d << " " << e << endl;
            //cout << p << " " << q << " " << r << " " << s << endl;
            //cout << max(p, max(q, max(r, s))) - min(p, min(q, min(r, s))) << endl;
            long long checker = max(p, max(q, max(r, s))) - min(p, min(q, min(r, s)));
            ans = min(ans, checker);
            //cout << ans << endl;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3712