Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
def spl_l(i,b):
    while abs(h[i]-2*h[b])>abs(h[i]-2*h[b+1]):
        b+=1
    return b
def spl_r(i,b):
    while abs(h[-1]-2*h[b]+h[i])>abs(h[-1]-2*h[b+1]+h[i]):
        b+=1
    return b
def calc(j,i,k,h):
    j=spl_l(i,j)
    k=spl_r(i,k)
    pqrs=[h[j],h[i]-h[j],h[k]-h[i],h[-1]-h[k]]
    ans=max(pqrs)-min(pqrs)
    return ans

j=0
k=2
ans=calc(j,1,k,h)
for i in range(2,N-2):
    ans=min(ans,calc(j,i,k,h))
print(ans)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
def spl_l(i,b):
    while abs(h[i]-2*h[b])>abs(h[i]-2*h[b+1]):
        b+=1
    return b
def spl_r(i,b):
    while abs(h[-1]-2*h[b]+h[i])>abs(h[-1]-2*h[b+1]+h[i]):
        b+=1
    return b
def calc(j,i,k):
    pqrs=[h[j],h[i]-h[j],h[k]-h[i],h[-1]-h[k]]
    ans=max(pqrs)-min(pqrs)
    return ans

j=0
k=2
ans=18**20
for i in range(1,N-2):
    j=spl_l(i,j)
    k=spl_r(i,k)
    ans=min(ans,calc(j,i,k))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def spl1(a):
    delta=[]
    for i in range(len(a)-1):
        delta.append(abs(a[-1]-2*a[i]))
    m=min(delta)
    index=[]
    for i in range(len(delta)):
        if delta[i]==m:
            index.append(i)
    return index
def spl2(a,b):
    delta=[]
    for i in range(len(a)-1):
        delta.append(abs(a[-1]-2*a[i]+b))
    m=min(delta)
    index=[]
    for i in range(len(delta)):
        if delta[i]==m:
            index.append(i)
    return index
N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
ans=10**18
for i in range(N):
    if i>0 and i<N-2:
        for j in spl1(h[:i+1]):
            for k in spl2(h[i+1:],h[i]):
                p=h[j]
                q=h[i]-h[j]
                r=h[k+i+1]-h[i]
                s=h[-1]-h[k+i+1]
                if (max([p,q,r,s])-min([p,q,r,s]))<ans:
                    ans=max([p,q,r,s])-min([p,q,r,s])
print(ans)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
def spl_l(i,b):
    while abs(h[i]-2*h[b])>abs(h[i]-2*h[b+1]):
        b+=1
    return b
def spl_r(i,b):
    while abs(h[-1]-2*h[b]+h[i])>abs(h[-1]-2*h[b+1]+h[i]):
        b+=1
    return b
def calc(j,i,k):
    pqrs=[h[j],h[i]-h[j],h[k]-h[i],h[-1]-h[k]]
    ans=max(pqrs)-min(pqrs)
    return ans

j=0
k=2
ans=18**20
for i in range(1,N-2):
    j=spl_l(i,j)
    k=spl_r(i,k)
    ans=min(ans,calc(j,i,k))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def spl(a):
    delta=[]
    for i in range(len(a)-1):
        delta.append(abs(a[-1]-2*a[i]))
    m=min(delta)
    index=[]
    for i in range(len(delta)):
        if delta[i]==m:
            index.append(i)
    return index

N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
ans=10**18
for i in range(N):
    if i>0 and i<N-2:
        g=[l-h[i] for l in h[i+1:]]
        for j in spl(h[:i+1]):
            for k in spl(g):
                p=h[j]
                q=h[i]-h[j]
                r=h[k+i+1]-h[i]
                s=h[-1]-h[k+i+1]
                if (max([p,q,r,s])-min([p,q,r,s]))<ans:
                    ans=max([p,q,r,s])-min([p,q,r,s])
print(ans)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
h=[0]
for a in A:
    h.append(a+h[-1])
h.pop(0)
def spl_l(i,b):
    while abs(h[i]-2*h[b])>abs(h[i]-2*h[b+1]):
        b+=1
    return b
def spl_r(i,b):
    while abs(h[-1]-2*h[b]+h[i])>abs(h[-1]-2*h[b+1]+h[i]):
        b+=1
    return b
def calc(j,i,k):
    pqrs=[h[j],h[i]-h[j],h[k]-h[i],h[-1]-h[k]]
    ans=max(pqrs)-min(pqrs)
    return ans

j=0
k=2
ans=18**20
for i in range(1,N-2):
    j=spl_l(i,j)
    k=spl_r(i,k)
    ans=min(ans,calc(j,i,k))
print(ans)