from itertools import accumulate

from bisect import bisect_right



N = int(eval(input()))

As = list(map(int, input().split()))



accAs = [0] + list(accumulate(As))



# As[iFr]～As[iTo]の和を求める

def getSum(iFr, iTo):

    return accAs[iTo+1] - accAs[iFr]





# As[iFr]～As[iTo]を前後半の総和の差が最小となるように分解する

def getDivision(iFr, iTo):

    sumB = getSum(iFr, iTo)

    iMid = bisect_right(accAs, accAs[iFr]+sumB/2, iFr+1, iTo+2) - 1



    diff1, diff2 = float('inf'), float('inf')

    if iMid != iFr:

        sum1L = getSum(iFr, iMid-1)

        sum1R = getSum(iMid, iTo)

        diff1 = abs(sum1L - sum1R)

    if iMid != iTo:

        sum2L = getSum(iFr, iMid)

        sum2R = getSum(iMid+1, iTo)

        diff2 = abs(sum2L - sum2R)



    return [sum1L, sum1R] if diff1 <= diff2 else [sum2L, sum2R]





# 真ん中の区切りを全通り試す

ans = float('inf')

for iMid in range(1, N-3+1):

    sumD = []

    sumD += getDivision(0, iMid)

    sumD += getDivision(iMid+1, N-1)



    ans = min(ans, max(sumD) - min(sumD))



print(ans)

N = int(eval(input()))

As = list(map(int, input().split()))



ans = float('inf')

iB, iD = 1, 3

(P, Q, R), S = As[:3], sum(As[3:])



# 真ん中の区切りを全通り試す

for iC in range(2, N - 1):

    while P < Q and As[iB] <= Q - P:

        P += As[iB]

        Q -= As[iB]

        iB += 1



    while R < S and As[iD] <= S - R:

        R += As[iD]

        S -= As[iD]

        iD += 1



    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))



    Q += As[iC]

    R -= As[iC]



print(ans)

from itertools import accumulate
from bisect import bisect_right

N = int(eval(input()))
As = list(map(int, input().split()))
accAs = [0] + list(accumulate(As))
# As[iFr]～As[iTo]の和を求める
def getSum(iFr, iTo):
    return accAs[iTo + 1] - accAs[iFr]


# As[iFr]～As[iTo]を前後半の総和の差が最小となるように分解する
def getDivision(iFr, iTo):
    sumB = getSum(iFr, iTo)
    iMid = bisect_right(accAs, accAs[iFr] + sumB / 2, iFr + 1, iTo + 2) - 1
    diff1, diff2 = float("inf"), float("inf")
    if iMid != iFr:
        sum1L = getSum(iFr, iMid - 1)
        sum1R = getSum(iMid, iTo)
        diff1 = abs(sum1L - sum1R)
    if iMid != iTo:
        sum2L = getSum(iFr, iMid)
        sum2R = getSum(iMid + 1, iTo)
        diff2 = abs(sum2L - sum2R)
    return [sum1L, sum1R] if diff1 <= diff2 else [sum2L, sum2R]


# 真ん中の区切りを全通り試す
ans = float("inf")
for iMid in range(1, N - 3 + 1):
    sumD = []
    sumD += getDivision(0, iMid)
    sumD += getDivision(iMid + 1, N - 1)
    ans = min(ans, max(sumD) - min(sumD))
print(ans)

N = int(eval(input()))
As = list(map(int, input().split()))
ans = float("inf")
iB, iD = 1, 3
(P, Q, R), S = As[:3], sum(As[3:])
# 真ん中の区切りを全通り試す
for iC in range(2, N - 1):
    while P < Q and As[iB] <= Q - P:
        P += As[iB]
        Q -= As[iB]
        iB += 1
    while R < S and As[iD] <= S - R:
        R += As[iD]
        S -= As[iD]
        iD += 1
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
    Q += As[iC]
    R -= As[iC]
print(ans)

-from itertools import accumulate -from bisect import bisect_right - -accAs = [0] + list(accumulate(As)) -# As[iFr]～As[iTo]の和を求める -def getSum(iFr, iTo): -    return accAs[iTo + 1] - accAs[iFr] - - -# As[iFr]～As[iTo]を前後半の総和の差が最小となるように分解する -def getDivision(iFr, iTo): -    sumB = getSum(iFr, iTo) -    iMid = bisect_right(accAs, accAs[iFr] + sumB / 2, iFr + 1, iTo + 2) - 1 -    diff1, diff2 = float("inf"), float("inf") -    if iMid != iFr: -        sum1L = getSum(iFr, iMid - 1) -        sum1R = getSum(iMid, iTo) -        diff1 = abs(sum1L - sum1R) -    if iMid != iTo: -        sum2L = getSum(iFr, iMid) -        sum2R = getSum(iMid + 1, iTo) -        diff2 = abs(sum2L - sum2R) -    return [sum1L, sum1R] if diff1 <= diff2 else [sum2L, sum2R] - - +ans = float("inf") +iB, iD = 1, 3 +(P, Q, R), S = As[:3], sum(As[3:]) -ans = float("inf") -for iMid in range(1, N - 3 + 1): -    sumD = [] -    sumD += getDivision(0, iMid) -    sumD += getDivision(iMid + 1, N - 1) -    ans = min(ans, max(sumD) - min(sumD)) +for iC in range(2, N - 1): +    while P < Q and As[iB] <= Q - P: +        P += As[iB] +        Q -= As[iB] +        iB += 1 +    while R < S and As[iD] <= S - R: +        R += As[iD] +        S -= As[iD] +        iD += 1 +    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S)) +    Q += As[iC] +    R -= As[iC]