using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using static System.Console;
using static PB10004.Algorithm.Util;

namespace PB10004.Algorithm
{
    class Template
    {
        static void Main(string[] args)
        {
            var N = ReadLong();
            var array = ReadLongArray();

            var cum = CumulativeSum(array).ToArray();

            long[] D1 = new long[N];
            long i = 0;
            long c = 0;
            while (i < N)
            {
                if (IsBestL(i, c, cum, N))
                {
                    D1[i] = c;
                    i++;
                }
                else
                    c++;
            }

            long[] D2 = new long[N];
            i = N - 1;
            c = N - 1;
            while (i >= 0)
            {
                if (IsBestR(i, c, cum, N))
                {
                    D2[i] = c;
                    i--;
                }
                else
                    c--;
            }
            long best = long.MaxValue;
            for (i = 0; i < N; i++)
            {
                long c1 = D1[i];
                long c2 = i;
                long c3 = D2[i];
                long c4 = N - 1;
                long p1 = cum[c1];
                long p2 = cum[c2] - cum[c1];
                long p3 = cum[c3] - cum[c2];
                long p4 = cum[c4] - cum[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)));
                best = Math.Min(best, mx - mn);
            }
            WriteLine(best);
        }
        static bool IsBestL(long i, long c, long[] S, long N)
        {
            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;
        }

        static bool IsBestR(long i, long c, long[] S, long N)
        {
            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;
        }
    }
    public static class Util
    {
        /// <summary>
        /// 標準入力をlong型にパースする
        /// </summary>
        /// <returns></returns>
        public static long ReadLong()
        {
            return long.Parse(ReadLine());
        }
        /// <summary>
        /// 標準入力をlong型の配列にする
        /// </summary>
        /// <returns></returns>
        public static long[] ReadLongArray()
        {
            return ReadLine().Split(' ').Select(long.Parse).ToArray();
        }
        /// <summary>
        /// 一定値の数列を生成する
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static IEnumerable<long> ConstSeq(long value, long length)
        {
            for (int i = 0; i < length; i++)
            {
                yield return value;
            }
        }
        /// <summary>
        /// int配列の累積和を求める
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static IEnumerable<int> CumulativeSum(IEnumerable<int> array)
        {
            int tmp = 0;
            foreach (var num in array)
                yield return tmp += num;
        }
        /// <summary>
        /// long配列の累積和を求める
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static IEnumerable<long> CumulativeSum(IEnumerable<long> array)
        {
            long tmp = 0;
            foreach (var num in array)
                yield return tmp += num;
        }
        /// <summary>
        /// 最大公約数をユークリッドの互助法で求める
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static long Gcd(long a, long b)
        {
            return b == 0 ? a : Gcd(b, a % b);
        }
        /// <summary>
        /// 最小公倍数を求める
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static long Lcm(long a, long b)
        {
            return a * b / Gcd(a, b);
        }
    }
}


Measured memory_bytes: 38940