#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;

#define X first
#define Y second

//#include <boost/unordered_map.hpp>
//using namespace boost;

/*
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
rbtree T;
*/

namespace io{
	const int L = (1 << 20) + 1;
	
	char buf[L], *S , *T, c;
	 
	char getchar() {
		if(S == T) {
			T = (S = buf) + fread(buf, 1, L, stdin);
			return (S == T ? EOF : *S++);
		}
		return *S++;
	}
	
	int inp() {
		int x = 0, f = 1; char ch;
		for(ch = getchar(); !isdigit(ch); ch = getchar())
			if(ch == '-') f = -1;
		for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		return x * f;
	}
	
	ll inp_ll() {
		ll x = 0; int f = 1; char ch;
		for(ch = getchar(); !isdigit(ch); ch = getchar())
			if(ch == '-') f = -1;
		for(; isdigit(ch); x = x * 10 + ch - '0', ch = getchar());
		return x * f;
	}
	
	char B[25], *outs=B+20, *outr=B+20;
	template<class T>
	inline void print(register T a,register char x=0){
		if(x) *--outs = x, x = 0;
		
		if(!a)*--outs = '0';
		else 
			while(a)
				*--outs = (a % 10) + 48, a /= 10;
		
		if(x)
			*--outs = x;
		
		fwrite(outs, outr - outs , 1, stdout);
		outs = outr;
	}
};

using io :: print;
using io :: inp;
using io :: inp_ll;

const int MAXN = 200010;
const ll INF = 10000000000000000LL;

int n;
int a[MAXN];
ll s[MAXN], sL[MAXN];
ll bL[MAXN], bR[MAXN];

ll myabs(ll a)
{
	return a >= 0 ? a : -a;
}

bool check(ll m)
{
	for (int i = 2; i <= n-2; ++ i)
	{
		ll sL = ::sL[i], sR = s[n-i];
		ll Lc = bL[i], Rc = bR[n-i];
		ll a = (sL+Lc)/2, b = (sL-Lc)/2, c = (sR+Rc)/2, d = (sR-Rc)/2;
		if (max(max(a, b), max(c, d)) - min(min(a, b), min(c, d)) <= m) return true;
	}
	return false;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++ i)
	{
		scanf("%d", &a[i]);
		s[i] = s[i-1]+a[i];
		sL[i] = s[i];
	}
	for (int i = 1; i <= n; ++ i)
	{
		int j = lower_bound(s+1, s+i+1, s[i]/2)-s;
		bL[i] = INF;
		if (1 <= j && j <= i-1)
		{
			int k = j;
			bL[i] = min(bL[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j-1 && j-1 <= i-1)
		{
			int k = j-1;
			bL[i] = min(bL[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j+1 && j+1 <= i-1)
		{
			int k = j;
			bL[i] = min(bL[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j-2 && j-2 <= i-1)
		{
			int k = j-1;
			bL[i] = min(bL[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j+2 && j+2 <= i-1)
		{
			int k = j;
			bL[i] = min(bL[i], myabs(s[i]-s[k] - s[k]));
		}
	}
	
	reverse(a+1, a+n+1);
	for (int i = 1; i <= n; ++ i)
	{
		s[i] = s[i-1]+a[i];
	}
	for (int i = 1; i <= n; ++ i)
	{
		int j = lower_bound(s+1, s+i+1, s[i]/2)-s;
		bR[i] = INF;
		if (1 <= j && j <= i-1)
		{
			int k = j;
			bR[i] = min(bR[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j-1 && j-1 <= i-1)
		{
			int k = j-1;
			bR[i] = min(bR[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j+1 && j+1 <= i-1)
		{
			int k = j;
			bR[i] = min(bR[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j-2 && j-2 <= i-1)
		{
			int k = j-1;
			bR[i] = min(bR[i], myabs(s[i]-s[k] - s[k]));
		}
		if (1 <= j+2 && j+2 <= i-1)
		{
			int k = j;
			bR[i] = min(bR[i], myabs(s[i]-s[k] - s[k]));
		}
	}
	ll L = 0, R = 1000000000LL*n;
	ll ans = R;
	while (L <= R)
	{
		ll m = (L+R)/2;
		if (check(m))
		{
			ans = m;
			R = m-1;
		}
		else
			L = m+1;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 7296