Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[int(i) for i in input().split()]

from itertools import accumulate
l=[0]+list(accumulate(a))
s=l[-1]
ans=10000000000000
import bisect
for i in range(2,N-1):
    ll=l[0:i+1]
    lr=l[i:N+1]
    lls=ll[-1]
    lrs=lr[-1]-lr[0]
    n=bisect.bisect_left(ll,lls//2)
    if n+1>=len(ll):
        n=len(ll)-2
    elif n+1<0:
        n=1
    if min(abs(lls-ll[n]*2),abs(lls-ll[n-1]*2),abs(lls-ll[n+1]*2))==abs(lls-ll[n]*2):
        s=ll[n]
        t=lls-ll[n]
    elif min(abs(lls-ll[n]*2),abs(lls-ll[n-1]*2),abs(lls-ll[n+1]*2))==abs(lls-ll[n-1]*2):
        s=ll[n-1]
        t=lls-ll[n-1]
    else:
        s=ll[n+1]
        t=lls-ll[n+1]
        
    
        
    n=bisect.bisect_left(lr,lr[0]+lrs//2)
    if n+1>=len(lr):
        n=len(lr)-2
    elif n-1<0:
        n=1
    if min(abs(lrs-lr[n]*2+2*lr[0]),abs(lrs-lr[n-1]*2+2*lr[0]),abs(lrs-lr[n+1]*2+2*lr[0]))==abs(lrs-lr[n]*2+2*lr[0]):
        u=lr[n]-lr[0]
        v=lrs-lr[n]+lr[0]
    elif min(abs(lrs-lr[n]*2+2*lr[0]),abs(lrs-lr[n-1]*2+2*lr[0]),abs(lrs-lr[n+1]*2+2*lr[0]))==abs(lrs-lr[n-1]*2+2*lr[0]):
        u=lr[n-1]-lr[0]
        v=lrs-lr[n-1]+lr[0]
    else:
        u=lr[n+1]-lr[0]
        v=lrs-lr[n+1]+lr[0]
    

    ans=min(ans,max(s,t,u,v)-min(s,t,u,v))

print(ans)
    
    







Fixed submission:
n=int(input())
a=[int(i) for i in input().split()]

for i in range(n-1):
    a[i+1]+=a[i]

j=0
k=2
ans=10**9

for i in range(1,n-1):
    while abs(a[j]*2-a[i])>abs(a[j+1]*2-a[i]):
        j+=1
    while abs(a[k]*2-a[i]-a[n-1])>abs(a[k+1]*2-a[i]-a[n-1]):
        k+=1
    t=[a[j],a[i]-a[j],a[k]-a[i],a[n-1]-a[k]]
    ans=min(ans,max(t)-min(t))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
a=[int(i) for i in input().split()]

from itertools import accumulate
l=[0]+list(accumulate(a))
s=l[-1]
ans=10000000000000
import bisect
for i in range(2,N-1):
    ll=l[0:i+1]
    lr=l[i:N+1]
    lls=ll[-1]
    lrs=lr[-1]-lr[0]
    n=bisect.bisect_left(ll,lls//2)
    if n+1>=len(ll):
        n=len(ll)-2
    elif n+1<0:
        n=1
    if min(abs(lls-ll[n]*2),abs(lls-ll[n-1]*2),abs(lls-ll[n+1]*2))==abs(lls-ll[n]*2):
        s=ll[n]
        t=lls-ll[n]
    elif min(abs(lls-ll[n]*2),abs(lls-ll[n-1]*2),abs(lls-ll[n+1]*2))==abs(lls-ll[n-1]*2):
        s=ll[n-1]
        t=lls-ll[n-1]
    else:
        s=ll[n+1]
        t=lls-ll[n+1]
        
    
        
    n=bisect.bisect_left(lr,lr[0]+lrs//2)
    if n+1>=len(lr):
        n=len(lr)-2
    elif n-1<0:
        n=1
    if min(abs(lrs-lr[n]*2+2*lr[0]),abs(lrs-lr[n-1]*2+2*lr[0]),abs(lrs-lr[n+1]*2+2*lr[0]))==abs(lrs-lr[n]*2+2*lr[0]):
        u=lr[n]-lr[0]
        v=lrs-lr[n]+lr[0]
    elif min(abs(lrs-lr[n]*2+2*lr[0]),abs(lrs-lr[n-1]*2+2*lr[0]),abs(lrs-lr[n+1]*2+2*lr[0]))==abs(lrs-lr[n-1]*2+2*lr[0]):
        u=lr[n-1]-lr[0]
        v=lrs-lr[n-1]+lr[0]
    else:
        u=lr[n+1]-lr[0]
        v=lrs-lr[n+1]+lr[0]
    

    ans=min(ans,max(s,t,u,v)-min(s,t,u,v))

print(ans)
    
    







Fixed submission:
n=int(input())
a=[int(i) for i in input().split()]

for i in range(n-1):
    a[i+1]+=a[i]

j=0
k=2
ans=10**9

for i in range(1,n-1):
    while abs(a[j]*2-a[i])>abs(a[j+1]*2-a[i]):
        j+=1
    while abs(a[k]*2-a[i]-a[n-1])>abs(a[k+1]*2-a[i]-a[n-1]):
        k+=1
    t=[a[j],a[i]-a[j],a[k]-a[i],a[n-1]-a[k]]
    ans=min(ans,max(t)-min(t))
print(ans)