#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rrep(i,n) for(int (i)=((n)-1);(i)>=0;(i)--)
#define itn int
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
const long long INF = 1LL << 60;
const int MOD = 1000000007;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//https://www.creativ.xyz/dump-cpp-652/
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
 
// vector
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec) is >> x;
    return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
    os << "(" << pair_var.first << ", " << pair_var.second << ")";
    return os;
}
// vector
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
    os << "{";
    repi(itr, map_var) {
        os << *itr;
        itr++;
        if (itr != map_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
// set
template <typename T>
ostream &operator<<(ostream &os, set<T> &set_var) {
    os << "{";
    repi(itr, set_var) {
        os << *itr;
        itr++;
        if (itr != set_var.end()) os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
 
#define DUMPOUT cerr
 
void dump_func() {
    DUMPOUT << endl;
}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
    DUMPOUT << head;
    if (sizeof...(Tail) > 0) {
        DUMPOUT << ", ";
    }
    dump_func(std::move(tail)...);
}

#ifdef DEBUG_
#define DEB
#define dump(...)                                                              \
    DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                            \
            << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]"        \
            << endl                                                            \
            << "    ",                                                         \
        dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif

template< typename T >
struct ruiseki{
    vector <T> s;
    int size;
    ruiseki(vector <T> &a){
        size = a.size()+1;
        s.resize(size);
        for(int i=1;i<size;i++) s[i] = s[i-1] + a[i-1];
    }
    ruiseki(){}
    T get(int a, int b){
        if(b > size-1) b = size-1;
        return s[b] - s[a];
    }
};
struct halve{
    struct ruiseki <int> r;
    halve(vector <int> &a){
        struct ruiseki <int> tmp(a);
        r = tmp;
    }
    tuple <int,int,int> get(int a, int b){ //半分わける場所、右側、左側を返す
        if(b > r.size) b = r.size;
        if(abs(a-b)<=1) return make_tuple(a,0,r.get(a, a+1));
        int sum = r.get(a, b);
        int left = a, right = b;
        while(abs(left - right)>1){
            int mid = (left +right)/2;
            if(r.get(a, mid+1) < sum/2){
                left = mid;
            }else right = mid;
        }
        int lsum1 = r.get(a, left+1);
        int rsum1 = sum - lsum1;
        int lsum2 = r.get(a, right+1);
        int rsum2 = sum - lsum2;
        if(abs(lsum1-rsum1)<abs(rsum2-lsum2)){
            return make_tuple(left+1, lsum1, rsum1);
        }else return make_tuple(right+1, lsum2, rsum2);
    }
};
signed main(void) { cin.tie(0); ios::sync_with_stdio(false);
    int n; cin>>n;
    vector <int> a(n);
    rep(i,n) cin>>a[i]; 
    struct halve half(a);
    int ans = INF;
    for(int i=0;i<n-1;i++){
        auto tmp = half.get(0,i);
        auto tmp2 = half.get(i,n);
        int n1=get <1>(tmp), n2 = get <2>(tmp),n3=get <1>(tmp2),n4=get <2>(tmp2);
        chmin(ans,max({n1,n2,n3,n4}) - min({n1,n2,n3,n4}));
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 4992