using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int N;
        public long[] A, S;
        public int[] D1, D2;
        // 中心の切る位置をiとした場合の、切る位置（左側）、切る位置（右側）
        // 切る位置は、[begin, c] [c+1, last]

        public void Run()
        {
            // Input
            N = Input.ReadInt();
            A = Input.ReadLongArray();

            S = new long[N];
            for (int i = 0; i < N; i++)
                S[i] = (i == 0 ? 0 : S[i - 1]) + A[i];

            // しゃくとり法
            D1 = new int[N];
            {
                int i = 0;
                int c = 0;
                while (i < N)
                {
                    if (IsBestL(i, c))
                    {
                        D1[i] = c;
                        i++;
                    }
                    else
                    {
                        c++;
                    }
                }
            }

            D2 = new int[N];
            {
                int i = N - 1;
                int c = N - 1;
                while (i >= 0)
                {
                    if (IsBestR(i, c))
                    {
                        D2[i] = c;
                        i--;
                    }
                    else
                    {
                        c--;
                    }
                }
            }


            long ret = long.MaxValue;
            for (int i = 0; i < N; i++)
            {
                int c1 = D1[i];
                int c2 = i;
                int c3 = D2[i];
                int c4 = N - 1;
                long p1 = S[c1]; ;
                long p2 = S[c2] - S[c1];
                long p3 = S[c3] - S[c2];
                long p4 = S[c4] - S[c3];

                long mn = Math.Min(p1, Math.Min(p2, Math.Min(p3, p4)));
                long mx = Math.Max(p1, Math.Max(p2, Math.Max(p3, p4)));
                ret = Math.Min(ret, mx - mn);
            }

            Console.WriteLine(ret);

        }

        public bool IsBestL(int i, int c)
        {
            if (c == i) return true;
            long s = S[i];
            long sc = S[c];
            long sn = S[c + 1];
            long current = Math.Abs(s - 2 * sc);
            long next = Math.Abs(s - 2 * sn);
            return current <= next;
        }

        public bool IsBestR(int i, int c)
        {
            if (c == i) return true;
            long s = S[N - 1] - S[i];
            long sc = S[N - 1] - S[c];
            long sn = S[N - 1] - S[c - 1];
            long current = Math.Abs(s - 2 * sc);
            long next = Math.Abs(s - 2 * sn);
            return current <= next;
        }

    }

    // libs ----------


    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }

    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}

Measured memory_bytes: 37084