#include <bits/stdc++.h>
using namespace std;
int main() {
  long long nums[200009], min_v = 1e9;
  int N;
  cin >> N;
  memset(nums, 0, sizeof(nums));
  long long v;
  for (int i = 1; i <= N; i++) {
    cin >> v;
    nums[i] += nums[i - 1] + v;
  }
  int l = 1, m = 2, r = 3;
  long long sum1, sum2, sum3, sum4;
  for (; m <= N; m++) {
    sum1 = nums[l];
    sum2 = nums[m] - nums[l];
    sum3 = nums[r] - nums[m];
    sum4 = nums[N] - nums[r];
    while (abs(sum4 - sum3) >
           abs((nums[N] - nums[r + 1]) - (nums[r + 1] - nums[m]))) {
      r++;
      sum3 = nums[r] - nums[m];
      sum4 = nums[N] - nums[r];
    }
    while (abs(sum2 - sum1) > abs((nums[m] - nums[l + 1]) - (nums[l + 1]))) {
      l++;
      sum1 = nums[l];
      sum2 = nums[m] - nums[l];
    }
    min_v = min(min_v,
                max({sum1, sum2, sum3, sum4}) - min({sum1, sum2, sum3, sum4}));
  }
  cout << min_v << endl;
  return 0;
}
