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

int n ;
LL ar[200005] ;

pair<LL,LL> BS(const int lower, const int upper){
	LL extra ;
	if(lower == 0)	extra =  0 ;
	else 	extra = ar[lower - 1] ;

	int l = lower, u = upper, mid, indx = -1 ;
	LL min_diff = LLONG_MAX ;

	while(l <= u){
		mid = (l + u) / 2 ;
		LL left = ar[mid] - extra ;
		LL right = ar[upper] - ar[mid] ;
		
		if(abs(left - right) < min_diff){
			min_diff = abs(left - right) ;
			indx = mid ;
		}
		
		if(left == right)
			break ;
		else if(left > right)
			u = mid - 1 ;
		else 
			l = mid + 1 ;
	}

	return {ar[indx] - extra, ar[upper] - ar[indx]} ;
}

int main(){

	cin >> n >> ar[0] ;
	for(int i = 1 ; i < n ; i++){
		cin >> ar[i] ;
		ar[i] += ar[i-1] ;
	}
	
	LL ans = LLONG_MAX ;
	for(int i = 1 ; i + 2 < n ; i++){
		pair<LL,LL> p1 = BS(0, i) ;
		pair<LL,LL> p2 = BS(i + 1, n - 1) ;
		ans = min(ans, ( max(p1.first, max(p1.second, max(p2.first, p2.second)))
					- min(p1.first, min(p1.second, min(p2.first, p2.second))) )) ;
	}
	cout << ans << endl ;

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

vector<int> calc_cut_left(vector<long long> pref, int n) {
  vector<int> ans(n);
  int cur = 0;
  ans[1] = 0;
  for (int i = 2; i + 2 < n; i++) {
    long long s1 = abs(pref[cur + 1] - (pref[i] - pref[cur + 1]));
    long long s2 = abs(pref[cur] - (pref[i] - pref[cur]));
    while (abs(pref[cur + 1] - (pref[i] - pref[cur + 1])) < abs(pref[cur] - (pref[i] - pref[cur])))
      cur++;
    ans[i] = cur;
  }
  return ans;
}

vector<int> calc_cut_right(vector<long long> pref, int n) {
  vector<int> ans(n);
  int cur = n - 2;
  ans[n - 3] = cur;
  for (int i = n - 4; i >= 1; i--) {
    long long s1 = abs((pref[n - 1] - pref[cur - 1]) - (pref[cur - 1] - pref[i]));
    long long s2 = abs((pref[n - 1] - pref[cur]) - (pref[cur] - pref[i]));
    while (abs((pref[n - 1] - pref[cur - 1]) - (pref[cur - 1] - pref[i])) < abs((pref[n - 1] - pref[cur]) - (pref[cur] - pref[i])))
      cur--;
    ans[i] = cur;
  }
  return ans;
}

int main() {
  int n;
  scanf("%d", &n);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  vector<long long> pref(n, a[0]);
  for (int i = 1; i < n; i++)
    pref[i] = pref[i - 1] + a[i];
  vector<int> pos_left = calc_cut_left(pref, n);
  vector<int> pos_right = calc_cut_right(pref, n);
  long long ans = -1;
  for (int i = 1; i + 2 < n; i++) {
    int l = pos_left[i];
    int r = pos_right[i];
    long long s1 = pref[l];
    long long s2 = pref[i] - pref[l];
    long long s3 = pref[r] - pref[i];
    long long s4 = pref[n - 1] - pref[r];
    long long cur = max({s1, s2, s3, s4}) - min({s1, s2, s3, s4});
    if (ans == -1 || cur < ans)
      ans = cur;
  }
  printf("%lld\n", ans);
  return 0;
}