# D

N = int(eval(input()))

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



# cumsum from left

a = 0

A_left = []

for i in range(N):

    a += A_list[i]

    A_left.append(a)

    

# cumsum from right

a = 0

A_right = []

for i in range(N):

    a += A_list[N-1-i]

    A_right.append(a)



def binary_search_left(s):

    m = 0

    l = 0

    r = N - 1

    

    if A_left[0] > s:

        return 0

    

    while True:

        m = (l + r) // 2

        

        if A_left[m] <= s and A_left[m+1] > s:

            break

        elif A_left[m] <= s:

            l = m

        else:

            r = m

    if s - A_left[m] <= A_left[m+1] - s:

        return m

    else:

        return m+1

    

def binary_search_right(s):

    m = 0

    l = 0

    r = N - 1

    

    if A_right[0] > s:

        return 0

    

    while True:

        m = (l + r) // 2

        

        if A_right[m] <= s and A_right[m+1] > s:

            break

        elif A_right[m] <= s:

            l = m

        else:

            r = m

    if s - A_right[m] <= A_right[m+1] - s:

        return m

    else:

        return m+1

    

# B | C split

res = A_left[N-1]

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

    

    m_l = binary_search_left(A_left[s-1]/2)

    a, b = A_left[m_l], A_left[s-1] - A_left[m_l]

    r = N - s 

    m_r = binary_search_right(A_right[r-1]/2)

    c, d = A_right[m_r], A_right[r-1] - A_right[m_r]

    

    r = max([a,b,c,d]) - min([a,b,c,d])

    if r < res:

        res = r

if N == 4:

    res = max(A_list) - min(A_list)

print(res)
# D

n = int(eval(input()))

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



# cumsum from left

a = 0

A_left = []

for i in range(n):

    a += A_list[i]

    A_left.append(a)

    

res = A_left[n-1]

i = 0

j = 1

k = 2

while j <= n - 3:

    while True:

        if abs(A_left[j] / 2 - A_left[i]) > abs(A_left[j] / 2 - A_left[i+1]):

            i += 1

        else:

            break

            

    while True:

        if abs((A_left[n-1] - A_left[j]) / 2 - (A_left[n-1] - A_left[k])) > abs((A_left[n-1] - A_left[j]) / 2 - (A_left[n-1] - A_left[k+1])):

            k += 1

        else:

            break

    # print(i, j, k)

    a, b, c, d = A_left[i], A_left[j]-A_left[i], A_left[k]-A_left[j], A_left[n-1]-A_left[k]

    r = max([a,b,c,d]) - min([a,b,c,d])

    if r < res:

        res = r

    j += 1

    

print(res)
# D
N = int(eval(input()))
A_list = list(map(int, input().split()))
# cumsum from left
a = 0
A_left = []
for i in range(N):
    a += A_list[i]
    A_left.append(a)
# cumsum from right
a = 0
A_right = []
for i in range(N):
    a += A_list[N - 1 - i]
    A_right.append(a)


def binary_search_left(s):
    m = 0
    l = 0
    r = N - 1
    if A_left[0] > s:
        return 0
    while True:
        m = (l + r) // 2
        if A_left[m] <= s and A_left[m + 1] > s:
            break
        elif A_left[m] <= s:
            l = m
        else:
            r = m
    if s - A_left[m] <= A_left[m + 1] - s:
        return m
    else:
        return m + 1


def binary_search_right(s):
    m = 0
    l = 0
    r = N - 1
    if A_right[0] > s:
        return 0
    while True:
        m = (l + r) // 2
        if A_right[m] <= s and A_right[m + 1] > s:
            break
        elif A_right[m] <= s:
            l = m
        else:
            r = m
    if s - A_right[m] <= A_right[m + 1] - s:
        return m
    else:
        return m + 1


# B | C split
res = A_left[N - 1]
for s in range(2, N - 1):
    m_l = binary_search_left(A_left[s - 1] / 2)
    a, b = A_left[m_l], A_left[s - 1] - A_left[m_l]
    r = N - s
    m_r = binary_search_right(A_right[r - 1] / 2)
    c, d = A_right[m_r], A_right[r - 1] - A_right[m_r]
    r = max([a, b, c, d]) - min([a, b, c, d])
    if r < res:
        res = r
if N == 4:
    res = max(A_list) - min(A_list)
print(res)

# D
n = int(eval(input()))
A_list = list(map(int, input().split()))
# cumsum from left
a = 0
A_left = []
for i in range(n):
    a += A_list[i]
    A_left.append(a)
res = A_left[n - 1]
i = 0
j = 1
k = 2
while j <= n - 3:
    while True:
        if abs(A_left[j] / 2 - A_left[i]) > abs(A_left[j] / 2 - A_left[i + 1]):
            i += 1
        else:
            break
    while True:
        if abs((A_left[n - 1] - A_left[j]) / 2 - (A_left[n - 1] - A_left[k])) > abs(
            (A_left[n - 1] - A_left[j]) / 2 - (A_left[n - 1] - A_left[k + 1])
        ):
            k += 1
        else:
            break
    # print(i, j, k)
    a, b, c, d = (
        A_left[i],
        A_left[j] - A_left[i],
        A_left[k] - A_left[j],
        A_left[n - 1] - A_left[k],
    )
    r = max([a, b, c, d]) - min([a, b, c, d])
    if r < res:
        res = r
    j += 1
print(res)

-N = int(eval(input())) +n = int(eval(input())) -for i in range(N): +for i in range(n): -# cumsum from right -a = 0 -A_right = [] -for i in range(N): -    a += A_list[N - 1 - i] -    A_right.append(a) - - -def binary_search_left(s): -    m = 0 -    l = 0 -    r = N - 1 -    if A_left[0] > s: -        return 0 +res = A_left[n - 1] +i = 0 +j = 1 +k = 2 +while j <= n - 3: -        m = (l + r) // 2 -        if A_left[m] <= s and A_left[m + 1] > s: +        if abs(A_left[j] / 2 - A_left[i]) > abs(A_left[j] / 2 - A_left[i + 1]): +            i += 1 +        else: -        elif A_left[m] <= s: -            l = m +    while True: +        if abs((A_left[n - 1] - A_left[j]) / 2 - (A_left[n - 1] - A_left[k])) > abs( +            (A_left[n - 1] - A_left[j]) / 2 - (A_left[n - 1] - A_left[k + 1]) +        ): +            k += 1 -            r = m -    if s - A_left[m] <= A_left[m + 1] - s: -        return m -    else: -        return m + 1 - - -def binary_search_right(s): -    m = 0 -    l = 0 -    r = N - 1 -    if A_right[0] > s: -        return 0 -    while True: -        m = (l + r) // 2 -        if A_right[m] <= s and A_right[m + 1] > s: -        elif A_right[m] <= s: -            l = m -        else: -            r = m -    if s - A_right[m] <= A_right[m + 1] - s: -        return m -    else: -        return m + 1 - - -# B | C split -res = A_left[N - 1] -for s in range(2, N - 1): -    m_l = binary_search_left(A_left[s - 1] / 2) -    a, b = A_left[m_l], A_left[s - 1] - A_left[m_l] -    r = N - s -    m_r = binary_search_right(A_right[r - 1] / 2) -    c, d = A_right[m_r], A_right[r - 1] - A_right[m_r] +    # print(i, j, k) +    a, b, c, d = ( +        A_left[i], +        A_left[j] - A_left[i], +        A_left[k] - A_left[j], +        A_left[n - 1] - A_left[k], +    ) -if N == 4: -    res = max(A_list) - min(A_list) +    j += 1