n = int(input())
a = [int(x) for x in input().split()]
s = [0] * n
s[0] = a[0]
for i in range(1, n):
s[i] = s[i-1] + a[i]
res = 1e47
def check(p1, p2):
global res
a = s[p1]
b = s[p2] - s[p1]
c = s[n-1] - s[p2]
cur = max(abs(a-b), max(abs(a-c), abs(b-c)))
res = min(res, cur)
ind = 2
s1 = a[1]
s2 = s[n-1] - s[1]
for i in range(n-1):
while True:
if s1 + a[ind] < s2 - a[ind]:
s1 += a[ind]
s2 -= a[ind]
ind += 1
else:
break
check(i, ind - 1)
check(i, ind)
s1 -= a[i+1]
print(res)