import sys

from itertools import accumulate

from bisect import bisect_left as bi_l



n, *a = list(map(int, sys.stdin.read().split()))



def main():

    s = list(accumulate(a))



    ans = float('inf')

    for center in range(1, n-2):

        pq = s[center]

        left = bi_l(s, pq / 2)

        if left == 0:

            p = s[0]

        else:

            if pq - s[left] < s[left-1]:

                left -= 1

            p = s[left]

        q = pq - p



        rt = s[-1] - pq

        right = bi_l(s, pq + rt / 2)

        if right == center + 1:

            r = s[center+1] - pq

        else:

            if rt - (s[right] - pq) < s[right-1] - pq:

                right -= 1

            r = s[right] - pq

        t = rt - r



        res = sorted([p, q, r, t])

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



    return ans



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys

from itertools import accumulate



n, *a = list(map(int, sys.stdin.read().split()))



def main():

    ans = float('inf')

    left = 0; right = 2

    p = a[0]; q = 0; r = sum(a[1:3]); s = sum(a[3:])

    for c in range(1, n-2):

        q += a[c]; r -= a[c]

        while left < c - 1:

            nex = a[left+1]

            if abs((q - nex) - (p + nex)) <= abs(q - p):

                q -= nex; p += nex

                left += 1

            else:

                break  

        

        while right < n - 2:

            nex = a[right+1]

            if abs((s - nex) - (r + nex)) <= abs(s - r):

                s -= nex; r += nex

                right += 1

            else:

                break



        res = sorted([p, q, r, s])

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



    return ans



if __name__ == '__main__':

    ans = main()

    print(ans)
import sys
from itertools import accumulate
from bisect import bisect_left as bi_l

n, *a = list(map(int, sys.stdin.read().split()))


def main():
    s = list(accumulate(a))
    ans = float("inf")
    for center in range(1, n - 2):
        pq = s[center]
        left = bi_l(s, pq / 2)
        if left == 0:
            p = s[0]
        else:
            if pq - s[left] < s[left - 1]:
                left -= 1
            p = s[left]
        q = pq - p
        rt = s[-1] - pq
        right = bi_l(s, pq + rt / 2)
        if right == center + 1:
            r = s[center + 1] - pq
        else:
            if rt - (s[right] - pq) < s[right - 1] - pq:
                right -= 1
            r = s[right] - pq
        t = rt - r
        res = sorted([p, q, r, t])
        ans = min(ans, res[-1] - res[0])
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

import sys
from itertools import accumulate

n, *a = list(map(int, sys.stdin.read().split()))


def main():
    ans = float("inf")
    left = 0
    right = 2
    p = a[0]
    q = 0
    r = sum(a[1:3])
    s = sum(a[3:])
    for c in range(1, n - 2):
        q += a[c]
        r -= a[c]
        while left < c - 1:
            nex = a[left + 1]
            if abs((q - nex) - (p + nex)) <= abs(q - p):
                q -= nex
                p += nex
                left += 1
            else:
                break
        while right < n - 2:
            nex = a[right + 1]
            if abs((s - nex) - (r + nex)) <= abs(s - r):
                s -= nex
                r += nex
                right += 1
            else:
                break
        res = sorted([p, q, r, s])
        ans = min(ans, res[-1] - res[0])
    return ans


if __name__ == "__main__":
    ans = main()
    print(ans)

-from bisect import bisect_left as bi_l -    s = list(accumulate(a)) -    for center in range(1, n - 2): -        pq = s[center] -        left = bi_l(s, pq / 2) -        if left == 0: -            p = s[0] -        else: -            if pq - s[left] < s[left - 1]: -                left -= 1 -            p = s[left] -        q = pq - p -        rt = s[-1] - pq -        right = bi_l(s, pq + rt / 2) -        if right == center + 1: -            r = s[center + 1] - pq -        else: -            if rt - (s[right] - pq) < s[right - 1] - pq: -                right -= 1 -            r = s[right] - pq -        t = rt - r -        res = sorted([p, q, r, t]) +    left = 0 +    right = 2 +    p = a[0] +    q = 0 +    r = sum(a[1:3]) +    s = sum(a[3:]) +    for c in range(1, n - 2): +        q += a[c] +        r -= a[c] +        while left < c - 1: +            nex = a[left + 1] +            if abs((q - nex) - (p + nex)) <= abs(q - p): +                q -= nex +                p += nex +                left += 1 +            else: +                break +        while right < n - 2: +            nex = a[right + 1] +            if abs((s - nex) - (r + nex)) <= abs(s - r): +                s -= nex +                r += nex +                right += 1 +            else: +                break +        res = sorted([p, q, r, s])