#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x, T a, T b) { return a <= x&&x <= b; }
template<class T> void amin(T &a, T v) { if (a > v) a = v; }
template<class T> void amax(T &a, T v) { if (a < v) a = v; }
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS*max(1.0, max(fabs(x), fabs(y))); }
double SQSUM(double x, double y) { return x*x + y*y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9+10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;

class RandXor
{
public:
	RandXor()
	{
		init();
	}

	void init()
	{
		x = 123456789;
		y = 362436069;
		z = 521288629;
		w = 88675123;
	}

	void init(ll seed)
	{
		x = 123456789;
		y = static_cast<unsigned int>(362436069 + seed * 674890201);
		z = static_cast<unsigned int>(521288629 - seed * seed * 164907679);
		w = static_cast<unsigned int>(88675123 * seed);
	}


	inline unsigned int rand()
	{
		unsigned int t;
		t = (x ^ (x << 11)); x = y; y = z; z = w; return(w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
	}

	unsigned int next() {
		return rand();
	}

	int nextInt(int num) {
		return rand() % num;
	}

	// [a,b)
	int next(int a, int b) {
		return a + (rand() % (b - a));
	}

	// [-d,d]
	int nextDelta(int d) {
		return next(-d, d + 1);
	}

	double nextDouble() {
		return (rand() + 0.5) * (1.0 / 4294967296.0);
	}

	template <class T>
	void randomShuffle(vector <T> &a)
	{
		const int n = SZ(a);
		for (int i = n - 1; i > 0; --i) {
			swap(a[i], a[nextInt(i + 1)]);
		}
	}

private:
	unsigned int x;
	unsigned int y;
	unsigned int z;
	unsigned int w;
};

int main(void) 
{
	int N;
	cin >> N;
	vector <ll> a(N);
	vector <ll> sum(N+1);
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
		sum[i + 1] = sum[i] + a[i];
	}

	ll best = BIGLL;
	const int d = 10;
	for (int b=2;b<=N-2;b++)
	{
		// 半分になりそうなあたり
		int x = lower_bound(sum.begin(), sum.end(), sum[b] / 2) - sum.begin();
		int y = lower_bound(sum.begin(), sum.end(), sum[b]+ (sum[N]-sum[b])/2) - sum.begin();

		for (int aa=x-d;aa<=x+d;++aa)
		{
			for (int cc = y - d; cc<=y + d; ++cc)
			{
				if (0 < aa && aa < b && b < cc && cc < N)
				{
					ll q = sum[aa] - sum[0];
					ll w = sum[b] - sum[aa];
					ll e = sum[cc] - sum[b];
					ll r = sum[N] - sum[cc];

					ll next = max({q,w,e,r}) - min({ q,w,e,r });
					amin(best, next);
				}
			}
		}

	}



#if 0
	vector <int> p = {1,N/2,N-1};

	ll now = 0;
	{
		ll a = sum[p[0]] - sum[0];
		ll b = sum[p[1]] - sum[p[0]];
		ll c = sum[p[2]] - sum[p[1]];
		ll d = sum[N] - sum[p[2]];
		now = max({ a,b,c,d }) - min({ a, b, c, d });
		amin(best, now);
	}


	int backp[3];

	ll T = 1e6;
	for (int loop = 0; loop < T; ++loop)
	{
		backp[0] = p[0];
		backp[1] = p[1];
		backp[2] = p[2];


		ll remains = 100 * (T - loop) / T;

		do
		{
			p[0] = backp[0];
			p[1] = backp[1];
			p[2] = backp[2];

			int id = rnd.nextInt(3);
			int d = rnd.nextDelta(remains);
			p[id] += d;

		} while (!(0 < p[0] && p[0] < p[1] && p[1] < p[2] && p[2] < N));


		if (next<now || loop < rnd.nextDouble()*T)
		{
			amin(best, next);
		}
		else
		{
			p[0] = backp[0];
			p[1] = backp[1];
			p[2] = backp[2];
		}
	}
#endif

	cout << best << endl;

	return 0;
}


Measured memory_bytes: 3328