//============================================================================
// Name        : hoba.cpp :)
// Author      : (O_O Diab) (Why_not) (el 3AQ)
// Version     :
// Copyright   : please, Don't touch me :(
// Description : problem in C++
// MAIN IDEA   :
//============================================================================
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define rw freopen("graphs_as_a_service.txt","r",stdin),freopen("output.txt","w",stdout);
#define pb push_back
#define F first
#define S second
#define pf push_front
#define lp(i,a,b) for (ll i=a;i<=(ll)b;++i)
#define lp2(i,a,b) for (int i=a;i>=b;--i)
#define f(x) return cout<<x<<"\n",0
#define pii pair<int,int>
#define pll pair<ll,ll>
#define all(c) c.begin(),c.end()
#define allr(c) c.rbegin(),c.rend()
#define p(x) cout<<#x<<" = { "<<x<<" } \n"
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define vll vector<pll>
#define rep(i,a) lp(i,0,a-1)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define EPS 1e-6
// to convert string to number use int a = stoi(string);
// to convert number to string use string s = to_string(int);
#define FAST ios::sync_with_stdio(0),ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define clr(c,x) memset(c,x,sizeof(c));
using namespace __gnu_pbds;
using namespace std;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
const int dy[] = { 1, 0, -1, 0, 1, -1, -1, 1 };
const int N = 2e5 + 3, mod = (int) 1e9 + 7, INF = 0x3f3f3f3f;
int a[N] = { }, n;
vector<pair<pii, pii>> vv;
void stress_test() {
	ll sum1 = 0;
	ll mx = LONG_MAX;
	vi v;
	lp(i,1,n-3)
	{
		sum1 += a[i];
		ll sum2 = 0;
		lp(j,i+1,n-2)
		{
			sum2 += a[j];
			ll sum3 = 0;
			lp(k,j+1,n-1)
			{
				sum3 += a[k];
				ll sum4 = 0;
				lp(z,k+1,n)
				{
					sum4 += a[z];
				}
				v.clear();
				v.pb(sum1);
				v.pb(sum2);
				v.pb(sum3);
				v.pb(sum4);
				sort(all(v));
				if (abs(v.back() - v[0]) < mx) {
					mx = v.back() - v[0];
//					cout << v.back() << " " << v[0] << "\n";
					vv.clear();
				}
				if (abs(v.back() - v[0]) == mx) {
					vv.pb( { { 1, i + 1 }, { j + 1, k + 1 } });
				}
			}
		}
	}
	cout << mx << "\n";
	for (auto u : vv)
		cout << u.F.S << " " << u.S.F << " " << u.S.S << "\n";
}
ll sum[N] = { };
ll lower(int l, int r, ll val) {
	ll ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (sum[mid] <= val) {
			l = mid + 1;
			ans = sum[mid];
		} else
			r = mid - 1;
	}
	return ans;
}
ll upper(int l, int r, ll val) {
	ll ans = 0;
	while (l <= r) {
		int mid = (l + r) >> 1;
		if (sum[mid] < val) {
			l = mid + 1;
		} else {
			r = mid - 1;
			ans = sum[mid];
		}
	}
	return ans;
}
int main() {
#ifdef ONLINE_JUDGE
	FAST
#endif
	cin >> n;
	lp(i,1,n)
	{
		cin >> a[i];
		sum[i] += (sum[i - 1] + a[i]);
	}
	ll ans = LONG_MAX;
	lp(i,2,n-2)
	{
		ll totl = sum[i];
		vll q1, q2;
		ll ff = lower(1, i, totl / 2);
		q1.pb( { ff, totl - ff });
		ff = upper(1, i, totl / 2);
		q1.pb( { ff, totl - ff });
		ll totr = sum[n] - sum[i];
		ff = lower(i + 1, n, sum[i] + totr / 2);
		q2.pb( { ff - sum[i], totr - (ff - sum[i]) });
		ff = upper(i + 1, n, sum[i] + totr / 2);
		q2.pb( { ff - sum[i], totr - (ff - sum[i]) });
		for (auto u : q1) {
			for (auto w : q2) {
				vl r;
				if (!u.F || !u.S || !w.F || !w.S)
					continue;
				r.pb(u.F);
				r.pb(u.S);
				r.pb(w.F);
				r.pb(w.S);
				sort(all(r));
				ans = min(ans, r.back() - r[0]);
			}
		}
	}
	cout << ans;
	cout << "\n";
	//stress_test();
	return 0;
}

Measured memory_bytes: 2560