using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private struct Pair
    {
        public long x, y;
        public Pair(long x, long y) { this.x = x; this.y = y; }
        public long Max() { return Math.Max(x, y); }
        public long Min() { return Math.Min(x, y); }
    }
    public static void Solve()
    {
        int N = NextInt();
        long[] A = LineLong();
        long[] B = new long[N + 1];
        for (int i = 1; i <= N; i++) B[i] += B[i - 1] + A[i - 1];
        long[] C = new long[N + 1];
        for (int i = 1; i <= N; i++) C[i] += C[i - 1] + A[N - i];
        //隙間はN-1個ある
        long ans = long.MaxValue;
        for (int mid = 1; mid < N - 2; mid++)
        {
            //なるべく均一にしたい
            //左側
            Pair[] lp = new Pair[2];
            int left_mid = Math.Max(LowerBound(B, B[mid + 1] / 2), 1);
            int right_mid = Math.Min(left_mid + 1, mid);

            lp[0] = new Pair(B[left_mid], B[mid + 1] - B[left_mid]);
            lp[1] = new Pair(B[right_mid], B[mid + 1] - B[right_mid]);

            Pair[] rp = new Pair[2];
            right_mid = Math.Max(LowerBound(C, C[N - mid - 1] / 2), 1);
            left_mid = Math.Min(right_mid + 1, N - mid - 2);

            rp[0] = new Pair(C[N - mid - 1] - C[right_mid], C[right_mid]);
            rp[1] = new Pair(C[N - mid - 1] - C[left_mid], C[left_mid]);

            for (int a = 0; a < 2; a++)
                for (int b = 0; b < 2; b++)
                {
                    ans =Math.Min(ans, Math.Max(lp[a].Max(), rp[b].Max())
                         - Math.Min(lp[a].Min(), rp[b].Min()));
                    //Console.WriteLine(lp[a].x + " " + lp[a].y + " "
                    //    + rp[b].x + " " + rp[b].y);
                }
        }
        Console.WriteLine(ans);

    }
    private static int LowerBound(long[] list, long n)
    {
        int a = 0, b = list.Count();
        if (list[b - 1] <= n) return b - 1;
        else if (list[a] > n) return -1;
        while (b - a != 1)
        {
            int x = (a + b) / 2;
            if (list[x] <= n) a = x;
            else b = x;
        }
        return a;
    }
    public static void Main()
    {
        var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        Solve();
        Console.Out.Flush();
    }
}
public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static char NextChar() { Confirm(); return char.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}


Measured memory_bytes: 37212