import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main() {
  int n;
  readf (" %d", &n);
  auto s = new int[n];
  foreach (t; 0 .. n) {
    readf (" %d", &s[t]);
    if (t) s[t] += s[t-1];
  }
  long[4] x;
  long[4] y;
  long[4] z;
  long res = long.max;
  bool go (int depth, int start, long ps, long rs) {
    if (depth == 3) {
      long t;
      if (rs < y[2]) {
        t = z[2] - rs;
      } else if (rs < z[2]) {
        t = z[2] - y[2];
      } else {
        t = rs - y[2];
      }
      if (res > t) res = t;
      return false;
    }
    if (depth && z[depth-1] - y[depth-1] >= res) return true;
    immutable parts = 4 - depth;
    auto d = s[start .. $].assumeSorted ();
    auto r = d.lowerBound (ps + rs / parts);
    immutable l = (start + r.length).to!int;
    int x1 = l;
    int x2 = l - 1;
    int stop = 0;
    bool next (int v) {
      x[depth] = s[v] - ps;
      if (depth) {
        y[depth] = min (y[depth-1], x[depth]);
        z[depth] = max (z[depth-1], x[depth]);
      } else {
        y[depth] = z[depth] = x[depth];
      }
      return go (depth + 1, v + 1, s[v], rs - x[depth]);
    }
    //depth == 2 => x <= n - 2
    //depth == 1 => x <= n - 3
    int b;
    do {
      b = 0;
      if (x1 <= n - (4 - depth)) {
        if (!next (x1)) b |= 1;
        ++x1;
      }
      if (x2 >= start) {
        if (!next (x2)) b |= 2;
        --x2;
      }
    } while (b);
    return false;
  }
  go (0, 0, 0L, s[n - 1]);
  writeln (res);
}
