#include <bits/stdc++.h>
using namespace std;

typedef long long lint;

int n, a[200004];
lint s[200004];

lint f(int x, int y, int z) {
	if (x>=y or y>=z) return 1e18;
	lint arr[4] = {s[x], s[y]-s[x], s[z]-s[y], s[n]-s[z]};
	return *max_element(arr, arr+4) - *min_element(arr, arr+4);
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i=0; i<n; i++) cin >> a[i];
	for (int i=1; i<=n; i++) s[i] = s[i-1] + a[i-1];

	lint ans = 1e18;
	for (int j=2; j<n-1; j++) {
		int i = lower_bound(s, s+n, (s[j]+1)/2) - s;
		int k = lower_bound(s, s+n, (s[j]+s[n]+1)/2) - s;
		ans = min({ans, f(i, j, k), f(i, j, k-1), f(i-1, j, k), f(i-1, j, k-1)});
	}

	cout << ans;
}
