#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[(1000)];
long long s[(1000)];
long long minmax4(long long a, long long b, long long c, long long d) {
  long long mn, mx;
  mn = min(a, b);
  mn = min(mn, c);
  mn = min(mn, d);
  mx = max(a, b);
  mx = max(mx, c);
  mx = max(mx, d);
  return mx - mn;
}
void print4(long long a, long long b, long long c, long long d) {
  cout << a << " " << b << " " << c << " " << d << "\n";
}
int main(void) {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    s[i] = s[i - 1] + a[i];
  }
  long long L, R;
  long long l = 1, r = 3;
  long long ans = (100000000000000);
  for (long long i = 2; i = n - 2; i++) {
    L = s[i];
    R = s[n] - s[i];
    while (abs(2 * s[l] - L) > abs(2 * s[l + 1] - L)) {
      if ((l + 1) == i) break;
      l++;
    }
    if (r < i + 1) r = i + 1;
    while (abs(2 * (s[r] - L) - R) > abs(2 * (s[r + 1] - L) - R)) {
      if ((r + 1) == n) break;
      r++;
    }
    ans = min(ans, minmax4(s[l], L - s[l], s[r] - L, s[n] - s[r]));
  }
  cout << ans << endl;
  return 0;
}
