Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
R = [0] * (N + 1)
for i in range(N):
  R[i + 1] += R[i] + A[i]
#print(R)  

ans = 10 ** 15
for i in range(3, N):#R[:i]とR[i:]に切る
  l, r = 2, i + 1
  #R[:l], R[l:i], R[i:r], R[r:]
  #R[l - 1] と R[i - 1] - R[l - 1] の差を考える
  
  while abs(R[i - 1] - 2 * R[l]) <= abs(R[i - 1] - 2 * R[l - 1]):
    l += 1
    
  #R[r - 1] - R [i - 1] と R[N] - R[r - 1] の差を考える  
  while abs(R[N] - 2 * R[r] + R[i - 1]) <= abs(R[N] - 2 * R[r - 1] + R[i - 1]):
    r += 1  
  
  now_max = max(max(R[l - 1], R[i - 1] - R[l - 1]), max(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  now_min = min(min(R[l - 1], R[i - 1] - R[l - 1]), min(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  ans = min(ans, now_max - now_min)
  #print(a, b, c, d, ans, ans2)
print(ans)
    
  
  

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
R = [0] * (N + 1)
for i in range(N):
  R[i + 1] += R[i] + A[i]
#print(R)  

ans = 10 ** 15
l, r = 2, 4
for i in range(3, N):#R[:i]とR[i:]に切る
  #R[:l], R[l:i], R[i:r], R[r:]
  #R[l - 1] と R[i - 1] - R[l - 1] の差を考える
  
  while abs(R[i - 1] - 2 * R[l]) <= abs(R[i - 1] - 2 * R[l - 1]):
    l += 1
    
  #R[r - 1] - R [i - 1] と R[N] - R[r - 1] の差を考える  
  while abs(R[N] - 2 * R[r] + R[i - 1]) <= abs(R[N] - 2 * R[r - 1] + R[i - 1]):
    r += 1  
  
  now_max = max(max(R[l - 1], R[i - 1] - R[l - 1]), max(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  now_min = min(min(R[l - 1], R[i - 1] - R[l - 1]), min(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  ans = min(ans, now_max - now_min)
  #print(R[l - 1], R[i - 1] - R[l - 1],R[r - 1] - R[i - 1], R[N] - R[r - 1], ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
R = [0] * (N + 1)
for i in range(N):
  R[i + 1] += R[i] + A[i]
#print(R)  

ans = 10 ** 15
for i in range(3, N):#R[:i]とR[i:]に切る
  #left = R[:i]
  #right = R[i]
  left, right = 10 ** 15, 10 ** 15
  a, b, c, d = 0, 0, 0, 0
  for j in range(2, i):#左側
    one = R[j - 1]
    two = R[i - 1] - one
    if left > abs(two - one):
      left = abs(two - one)
      a, b = one, two
    
  for j in range(i + 1, N + 1):#右側  
    three = R[j - 1] - R[i - 1]
    four = R[N] - R[j - 1]
    if right > abs(three - four):
      right = abs(three - four)
      c, d = three, four
    
  T = [a, b, c, d]  
  ans2 = max(T) - min(T)
  ans = min(ans, ans2)
  #print(a, b, c, d, ans, ans2)
print(ans)
    
  
  

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
R = [0] * (N + 1)
for i in range(N):
  R[i + 1] += R[i] + A[i]
#print(R)  

ans = 10 ** 15
l, r = 2, 4
for i in range(3, N):#R[:i]とR[i:]に切る
  #R[:l], R[l:i], R[i:r], R[r:]
  #R[l - 1] と R[i - 1] - R[l - 1] の差を考える
  
  while abs(R[i - 1] - 2 * R[l]) <= abs(R[i - 1] - 2 * R[l - 1]):
    l += 1
    
  #R[r - 1] - R [i - 1] と R[N] - R[r - 1] の差を考える  
  while abs(R[N] - 2 * R[r] + R[i - 1]) <= abs(R[N] - 2 * R[r - 1] + R[i - 1]):
    r += 1  
  
  now_max = max(max(R[l - 1], R[i - 1] - R[l - 1]), max(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  now_min = min(min(R[l - 1], R[i - 1] - R[l - 1]), min(R[r - 1] - R[i - 1], R[N] - R[r - 1]))
  ans = min(ans, now_max - now_min)
  #print(R[l - 1], R[i - 1] - R[l - 1],R[r - 1] - R[i - 1], R[N] - R[r - 1], ans)
print(ans)