Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
tmp=0
ca=[tmp]
for x in a:
  tmp+=x
  ca.append(tmp)
from bisect import bisect_left as bl
for i in range(2,n-1):
  bc=ca[:i+1]
  de=ca[i+1:]
  de=[0]+[x-ca[i] for x in de]
  sumbc=bc[-1]
  bcidx=bl(bc,sumbc//2)
  p=bc[bcidx]
  q=bc[-1]-bc[bcidx]
  if bcidx>1:
    np=bc[bcidx-1]
    nq=bc[-1]-bc[bcidx-1]
    if abs(p-q)>abs(np-nq):p,q=np,nq

  sumde=de[-1]
  deidx=bl(de,sumde//2)
  r=de[deidx]
  s=de[-1]-de[deidx]
  if deidx>1:
    nr=de[deidx-1]
    ns=de[-1]-de[deidx-1]
    if abs(r-s)>abs(nr-ns):r,s=nr,ns
  ans=min(ans,max(p,q,r,s)-min(p,q,r,s))
print(ans)



Fixed submission:
n=int(input())
s=list(map(int,input().split()))
a=[0]
c=0
for si in s:
    c+=si
    a.append(c)
ans=[]
f=1
g=3
for i in range(2,n-1):
    while abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans.append(max(ls)-min(ls))
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
tmp=0
ca=[tmp]
for x in a:
  tmp+=x
  ca.append(tmp)
from bisect import bisect_left as bl
for i in range(2,n-1):
  bc=ca[:i+1]
  de=ca[i+1:]
  de=[0]+[x-ca[i] for x in de]
  sumbc=bc[-1]
  bcidx=bl(bc,sumbc//2)
  p=bc[bcidx]
  q=bc[-1]-bc[bcidx]
  if bcidx>1:
    np=bc[bcidx-1]
    nq=bc[-1]-bc[bcidx-1]
    if abs(p-q)>abs(np-nq):p,q=np,nq

  sumde=de[-1]
  deidx=bl(de,sumde//2)
  r=de[deidx]
  s=de[-1]-de[deidx]
  if deidx>1:
    nr=de[deidx-1]
    ns=de[-1]-de[deidx-1]
    if abs(r-s)>abs(nr-ns):r,s=nr,ns
  ans=min(ans,max(p,q,r,s)-min(p,q,r,s))
print(ans)



Fixed submission:
n=int(input())
s=list(map(int,input().split()))
a=[0]
c=0
for si in s:
    c+=si
    a.append(c)
ans=[]
f=1
g=3
for i in range(2,n-1):
    while abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans.append(max(ls)-min(ls))
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
s=list(map(int,input().split()))
a=[0]
for _ in range(n):
    a.append(sum(s[:_+1]))

ans=10**9
f=1
g=3
for i in range(2,n-1):
    while f < i and abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while g < n and abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans=min(ans,max(ls)-min(ls))
print(ans)

Fixed submission:
n=int(input())
s=list(map(int,input().split()))
a=[0]
c=0
for si in s:
    c+=si
    a.append(c)
ans=[]
f=1
g=3
for i in range(2,n-1):
    while abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans.append(max(ls)-min(ls))
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
ans=10**9
f=1
g=3
for i in range(2,n-1):
    while f < i and abs(sum(a[:f])-sum(a[f:i])) > abs(sum(a[:f+1])-sum(a[f+1:i])):
        f+=1
    while g < n and abs(sum(a[i:g])-sum(a[g:])) > abs(sum(a[i:g+1])-sum(a[g+1:])):
        g+=1
    ans=min(ans,max(abs(sum(a[:f])-sum(a[f:i])),abs(sum(a[:f])-sum(a[i:g])),abs(sum(a[:f])-sum(a[g:]))
                ,abs(sum(a[f:i])-sum(a[i:g])),abs(sum(a[f:i])-sum(a[g:])),abs(sum(a[i:g])-sum(a[g:]))))
print(ans)

Fixed submission:
n=int(input())
s=list(map(int,input().split()))
a=[0]
c=0
for si in s:
    c+=si
    a.append(c)
ans=[]
f=1
g=3
for i in range(2,n-1):
    while abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans.append(max(ls)-min(ls))
print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
from collections import deque
ans=10**9
f=1
g=3
for i in range(2,n-1):
    l=a[:i]
    r=a[i:]
    k1=deque(a[:f])
    k2=deque(a[f:i])
    k3=deque(a[i:g])
    k4=deque(a[g:])
    while len(k2) > 1 and abs(sum(k1)-sum(k2)) > abs((sum(k1)+k2[0])-(sum(k2)-k2[0])):
        k1.append(k2.popleft())
        f+=1
    while len(k4) > 1 and abs(sum(k3)-sum(k4)) > abs((sum(k3)+k4[0])-(sum(k4)-k4[0])):
        k3.append(k4.popleft())
        g+=1
    ans=min(ans,max(abs(sum(k1)-sum(k2)),abs(sum(k1)-sum(k3)),abs(sum(k1)-sum(k4))
                ,abs(sum(k2)-sum(k3)),abs(sum(k2)-sum(k4)),abs(sum(k3)-sum(k4))))
print(ans)

Fixed submission:
n=int(input())
s=list(map(int,input().split()))
a=[0]
c=0
for si in s:
    c+=si
    a.append(c)
ans=[]
f=1
g=3
for i in range(2,n-1):
    while abs(a[f]-(a[i]-a[f])) > abs(a[f+1]-(a[i]-a[f+1])):
        f+=1
    while abs(a[g]-a[i]-(a[n]-a[g])) > abs(a[g+1]-a[i]-(a[n]-a[g+1])):
        g+=1
    ls=[a[f],a[i]-a[f],a[g]-a[i],a[n]-a[g]]
    ans.append(max(ls)-min(ls))
print(min(ans))