from itertools import accumulate





def solve(string):

    n, *a = list(map(int, string.split()))

    *a, = accumulate(a)

    l, c, r = 0, 1, 2

    sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r]

    ans = max(sll, slr, srl, srr) - min(sll, slr, srl, srr)

    while c < n - 1:

        while abs(a[c] - 2 * a[l + 1]) < abs(a[c] - 2 * a[l]):

            l += 1

        while abs(a[-1] + a[c] - 2 * a[r + 1]) < abs(a[-1] + a[c] - 2 * a[r]):

            r += 1

        sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r]

        ans = min(ans, max(sll, slr, srl, srr) - min(sll, slr, srl, srr))

        c+=1

    return str(ans)





if __name__ == '__main__':

    print((solve('\n'.join([eval(input()), eval(input())]))))

from itertools import accumulate





def solve(string):

    n, *a = list(map(int, string.split()))

    *a, = accumulate(a)

    l, r, e = 0, 2, a[-1]

    ans = e

    for c in a[1:-2]:

        while a[l] + a[l + 1] < c:

            l += 1

        while a[r] + a[r + 1] < c + e:

            r += 1

        tmp = sorted([a[l], c - a[l], a[r] - c, e - a[r]])

        ans = min(ans, tmp[-1] - tmp[0])

    return str(ans)





if __name__ == '__main__':

    print((solve('\n'.join([eval(input()), eval(input())]))))

from itertools import accumulate


def solve(string):
    n, *a = list(map(int, string.split()))
    (*a,) = accumulate(a)
    l, c, r = 0, 1, 2
    sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r]
    ans = max(sll, slr, srl, srr) - min(sll, slr, srl, srr)
    while c < n - 1:
        while abs(a[c] - 2 * a[l + 1]) < abs(a[c] - 2 * a[l]):
            l += 1
        while abs(a[-1] + a[c] - 2 * a[r + 1]) < abs(a[-1] + a[c] - 2 * a[r]):
            r += 1
        sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r]
        ans = min(ans, max(sll, slr, srl, srr) - min(sll, slr, srl, srr))
        c += 1
    return str(ans)


if __name__ == "__main__":
    print((solve("\n".join([eval(input()), eval(input())]))))

from itertools import accumulate


def solve(string):
    n, *a = list(map(int, string.split()))
    (*a,) = accumulate(a)
    l, r, e = 0, 2, a[-1]
    ans = e
    for c in a[1:-2]:
        while a[l] + a[l + 1] < c:
            l += 1
        while a[r] + a[r + 1] < c + e:
            r += 1
        tmp = sorted([a[l], c - a[l], a[r] - c, e - a[r]])
        ans = min(ans, tmp[-1] - tmp[0])
    return str(ans)


if __name__ == "__main__":
    print((solve("\n".join([eval(input()), eval(input())]))))

-    l, c, r = 0, 1, 2 -    sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r] -    ans = max(sll, slr, srl, srr) - min(sll, slr, srl, srr) -    while c < n - 1: -        while abs(a[c] - 2 * a[l + 1]) < abs(a[c] - 2 * a[l]): +    l, r, e = 0, 2, a[-1] +    ans = e +    for c in a[1:-2]: +        while a[l] + a[l + 1] < c: -        while abs(a[-1] + a[c] - 2 * a[r + 1]) < abs(a[-1] + a[c] - 2 * a[r]): +        while a[r] + a[r + 1] < c + e: -        sll, slr, srl, srr = a[l], a[c] - a[l], a[r] - a[c], a[-1] - a[r] -        ans = min(ans, max(sll, slr, srl, srr) - min(sll, slr, srl, srr)) -        c += 1 +        tmp = sorted([a[l], c - a[l], a[r] - c, e - a[r]]) +        ans = min(ans, tmp[-1] - tmp[0])