import sys

from itertools import accumulate

from bisect import bisect

def main():

    input = sys.stdin.readline

    N = int(eval(input()))

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

    ac = [0] + list(accumulate(A))



    def calc(lst): return abs(max(lst) - min(lst))

    def sm(l,r): return ac[r+1] - ac[l]

    def get_min_max(l,r):

        if r-l <= 1: return [A[l], A[r]]

        med = ac[l] + sm(l,r) / 2

        i = bisect(ac, med, l+1, r+1)

        r1 = [sm(l,i-1), sm(i,r)]

        if i-1 == l: return r1

        r2 = [sm(l,i-2), sm(i-1,r)]

        return r1 if calc(r1) < calc(r2) else r2



    ans = 10**18

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

        PQ_mm = get_min_max(0, R_left-1)

        RS_mm = get_min_max(R_left, N-1)

        ans = min(ans, calc(PQ_mm + RS_mm))

    print(ans)



if __name__ == '__main__':

    main()

import sys

from itertools import accumulate

from bisect import bisect

def main():

    input = sys.stdin.readline

    N = int(eval(input()))

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

    ac = [0] + list(accumulate(A))



    def calc(lst): return abs(max(lst) - min(lst))

    def sm(l,r): return ac[r+1] - ac[l]



    ans = 10**18

    Q_left, S_left = 1, 3

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

        P, Q = ac[Q_left], sm(Q_left, R_left-1)

        for nql in range(Q_left+1, R_left):

            nP, nQ = ac[nql], sm(nql, R_left-1)

            if calc([nP, nQ]) > calc([P, Q]): break

            Q_left, P, Q = nql, nP, nQ

        R, S = sm(R_left, S_left-1), sm(S_left, N-1)

        for nsl in range(S_left+1, N):

            nR, nS = sm(R_left, nsl-1), sm(nsl, N-1)

            if calc([nR, nS]) > calc([R, S]): break

            S_left, R, S = nsl, nR, nS

        ans = min(ans, calc([P,Q,R,S]))

    print(ans)



if __name__ == '__main__':

    main()

import sys
from itertools import accumulate
from bisect import bisect


def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    A = list(map(int, input().split()))
    ac = [0] + list(accumulate(A))

    def calc(lst):
        return abs(max(lst) - min(lst))

    def sm(l, r):
        return ac[r + 1] - ac[l]

    def get_min_max(l, r):
        if r - l <= 1:
            return [A[l], A[r]]
        med = ac[l] + sm(l, r) / 2
        i = bisect(ac, med, l + 1, r + 1)
        r1 = [sm(l, i - 1), sm(i, r)]
        if i - 1 == l:
            return r1
        r2 = [sm(l, i - 2), sm(i - 1, r)]
        return r1 if calc(r1) < calc(r2) else r2

    ans = 10**18
    for R_left in range(2, N - 1):
        PQ_mm = get_min_max(0, R_left - 1)
        RS_mm = get_min_max(R_left, N - 1)
        ans = min(ans, calc(PQ_mm + RS_mm))
    print(ans)


if __name__ == "__main__":
    main()

import sys
from itertools import accumulate
from bisect import bisect


def main():
    input = sys.stdin.readline
    N = int(eval(input()))
    A = list(map(int, input().split()))
    ac = [0] + list(accumulate(A))

    def calc(lst):
        return abs(max(lst) - min(lst))

    def sm(l, r):
        return ac[r + 1] - ac[l]

    ans = 10**18
    Q_left, S_left = 1, 3
    for R_left in range(2, N - 1):
        P, Q = ac[Q_left], sm(Q_left, R_left - 1)
        for nql in range(Q_left + 1, R_left):
            nP, nQ = ac[nql], sm(nql, R_left - 1)
            if calc([nP, nQ]) > calc([P, Q]):
                break
            Q_left, P, Q = nql, nP, nQ
        R, S = sm(R_left, S_left - 1), sm(S_left, N - 1)
        for nsl in range(S_left + 1, N):
            nR, nS = sm(R_left, nsl - 1), sm(nsl, N - 1)
            if calc([nR, nS]) > calc([R, S]):
                break
            S_left, R, S = nsl, nR, nS
        ans = min(ans, calc([P, Q, R, S]))
    print(ans)


if __name__ == "__main__":
    main()

-    def get_min_max(l, r): -        if r - l <= 1: -            return [A[l], A[r]] -        med = ac[l] + sm(l, r) / 2 -        i = bisect(ac, med, l + 1, r + 1) -        r1 = [sm(l, i - 1), sm(i, r)] -        if i - 1 == l: -            return r1 -        r2 = [sm(l, i - 2), sm(i - 1, r)] -        return r1 if calc(r1) < calc(r2) else r2 - +    Q_left, S_left = 1, 3 -        PQ_mm = get_min_max(0, R_left - 1) -        RS_mm = get_min_max(R_left, N - 1) -        ans = min(ans, calc(PQ_mm + RS_mm)) +        P, Q = ac[Q_left], sm(Q_left, R_left - 1) +        for nql in range(Q_left + 1, R_left): +            nP, nQ = ac[nql], sm(nql, R_left - 1) +            if calc([nP, nQ]) > calc([P, Q]): +                break +            Q_left, P, Q = nql, nP, nQ +        R, S = sm(R_left, S_left - 1), sm(S_left, N - 1) +        for nsl in range(S_left + 1, N): +            nR, nS = sm(R_left, nsl - 1), sm(nsl, N - 1) +            if calc([nR, nS]) > calc([R, S]): +                break +            S_left, R, S = nsl, nR, nS +        ans = min(ans, calc([P, Q, R, S]))