n = int(eval(input()))

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



cumsum = [a[0]]

for i in range(1, n):

    cumsum.append(cumsum[i-1] + a[i])



i, k = 0, 2

ans = 10**18

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

    while i < j:

        p = cumsum[i]

        q = cumsum[j] - cumsum[i]

        p_new = cumsum[i+1]

        q_new = cumsum[j] - cumsum[i+1]

        if abs(q_new - p_new) > abs(q - p):

            break

        else:

            i += 1



    while k < n-1:

        r = cumsum[k] - cumsum[j]

        s = cumsum[n-1] - cumsum[k]

        r_new = cumsum[k+1] - cumsum[j]

        s_new = cumsum[n-1] - cumsum[k+1]

        if abs(s_new - r_new) > abs(s - r):

            break

        else:

            k += 1



    pqrs = [p, q, r, s]

    ans = min(ans, abs(max(pqrs) - min(pqrs)))

    # print(i, j, k, ans, pqrs)

    

print(ans)

n = int(eval(input()))

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

 

i, k = 0, 2

p, q, r, s = 0, a[0], a[1], sum(a[2:])

ans = sum(a)

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

    q += a[j]

    while abs((q-a[i]) - (p+a[i])) < abs(q - p):

        p += a[i]

        q -= a[i]

        i += 1

 

    r -= a[j]

    while abs((s-a[k]) - (r+a[k])) < abs(s - r):

        r += a[k]

        s -= a[k]

        k += 1

 

    pqrs = [p, q, r, s]

    ans = min(ans, abs(max(pqrs) - min(pqrs)))

    # print(i, j, k, ans, pqrs)

    

print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
cumsum = [a[0]]
for i in range(1, n):
    cumsum.append(cumsum[i - 1] + a[i])
i, k = 0, 2
ans = 10**18
for j in range(1, n - 2):
    while i < j:
        p = cumsum[i]
        q = cumsum[j] - cumsum[i]
        p_new = cumsum[i + 1]
        q_new = cumsum[j] - cumsum[i + 1]
        if abs(q_new - p_new) > abs(q - p):
            break
        else:
            i += 1
    while k < n - 1:
        r = cumsum[k] - cumsum[j]
        s = cumsum[n - 1] - cumsum[k]
        r_new = cumsum[k + 1] - cumsum[j]
        s_new = cumsum[n - 1] - cumsum[k + 1]
        if abs(s_new - r_new) > abs(s - r):
            break
        else:
            k += 1
    pqrs = [p, q, r, s]
    ans = min(ans, abs(max(pqrs) - min(pqrs)))
    # print(i, j, k, ans, pqrs)
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
i, k = 0, 2
p, q, r, s = 0, a[0], a[1], sum(a[2:])
ans = sum(a)
for j in range(1, n - 2):
    q += a[j]
    while abs((q - a[i]) - (p + a[i])) < abs(q - p):
        p += a[i]
        q -= a[i]
        i += 1
    r -= a[j]
    while abs((s - a[k]) - (r + a[k])) < abs(s - r):
        r += a[k]
        s -= a[k]
        k += 1
    pqrs = [p, q, r, s]
    ans = min(ans, abs(max(pqrs) - min(pqrs)))
    # print(i, j, k, ans, pqrs)
print(ans)

-cumsum = [a[0]] -for i in range(1, n): -    cumsum.append(cumsum[i - 1] + a[i]) -ans = 10**18 +p, q, r, s = 0, a[0], a[1], sum(a[2:]) +ans = sum(a) -    while i < j: -        p = cumsum[i] -        q = cumsum[j] - cumsum[i] -        p_new = cumsum[i + 1] -        q_new = cumsum[j] - cumsum[i + 1] -        if abs(q_new - p_new) > abs(q - p): -            break -        else: -            i += 1 -    while k < n - 1: -        r = cumsum[k] - cumsum[j] -        s = cumsum[n - 1] - cumsum[k] -        r_new = cumsum[k + 1] - cumsum[j] -        s_new = cumsum[n - 1] - cumsum[k + 1] -        if abs(s_new - r_new) > abs(s - r): -            break -        else: -            k += 1 +    q += a[j] +    while abs((q - a[i]) - (p + a[i])) < abs(q - p): +        p += a[i] +        q -= a[i] +        i += 1 +    r -= a[j] +    while abs((s - a[k]) - (r + a[k])) < abs(s - r): +        r += a[k] +        s -= a[k] +        k += 1