#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string> 
#include <iomanip>
#include <list>
#include <random>
#include <numeric> //iota - iota(v.begin(), v.end(), x) x, x + 1, x + 2...

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef int INT;
#define int ll
#define mmin(a, b) a = min(a, b)
#define mmax(a, b) a = max(a, b)
#define all(a) a.begin(),a.end()
#define sz(a) ((int)(a.size()))
#define watch(x) cout << (#x) << " = " << x << endl;
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

/*
tuple<int, string> f() {
return {485, "Hello"};
}
int a;
string b;
tie(a, b) = f();
*/

const double PI = 2 * acos(0.0), eps = 1e-8;
const int oo = 1000000000000000000;

template<class T>
void show(const vector<T> &a) {
	cout << sz(a) << endl;
	for (T x : a)
		cout << x << " ";
	cout << endl;
}

const ll M = 998244353;

ll MOD(ll a) {
	if (a >= 0) return a >= M ? a % M : a;
	if (a <= -M) a %= M;
	return a ? a + M : a;
}

int gcd(int a, int b)
{
	while (a > 0)
	{
		if (a < b)
			swap(a, b);
		a %= b;
	}
	return b;
}


/*______________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
________________________________________________________________________________________________________________________________
______________________________________________________________________________________________________________________________*/



signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	int n;
	cin >> n;
	vector <int> a(n), prev(n), suff(n);

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (i != 0)
			prev[i] = prev[i - 1];
		prev[i] += a[i];
	}
	for (int i = n - 1; i >= 0; i--)
	{
		if (i != n - 1)
			suff[i] = suff[i + 1];
		suff[i] += a[i];
	}

	vector <int> on(n, -1), th(n, -1);
	int j = 0;
	for (int i = 1; i < n - 2; i++)
	{
		int v = prev[i] - prev[j], u = prev[j];
		int y = abs(v - u);
		while (j + 1 < i)
		{
			v = prev[i] - prev[j + 1];
			u = prev[j + 1];
			if (abs(v - u) <= y)
			{
				y = abs(v - u);
				j++;
			}
			else
				break;
		}
		on[i] = j;
	}
	j = n - 1;
	for (int i = n - 3; i >= 1; i--)
	{
		int v = suff[i + 1] - suff[j], u = suff[j];
		int y = abs(v - u);
		while (j - 1 > i + 1)
		{
			v = suff[i + 1] - suff[j - 1];
			u = suff[j - 1];
			if (abs(v - u) <= y)
			{
				y = abs(v - u);
				j--;
			}
			else
				break;
		}
		th[i] = j - 1;
	}
	int h = oo;
	for (int i = 1; i <= n - 3; i++)
	{
		int o1 = on[i], o2 = i, o3 = th[i];
		//cout << o1 << " " << o2 << " " << o3 << endl;
		int ch1 = prev[o1], ch2 = prev[o2] - prev[o1], ch3 = prev[o3] - prev[o2], ch4 = prev[n - 1] - prev[o3];
		//cout << ch1 << " " << ch2 << " " << ch3 << " " << ch4 << endl;
		int mn = min3(ch1, ch2, min(ch3, ch4)), mx = max3(ch1, ch2, max(ch3, ch4));
		//cout << mn << " " << mx << endl << endl;
		mmin(h, mx - mn);
	}

	cout << h;


}

Measured memory_bytes: 8064