Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect

n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, n):
	b.append(b[-1] + a[i])
#print(b)

ans = float("inf")
for i in range(2, n - 1):
	former_cut = bisect(b[:i], b[i-1] / 2)
	if former_cut == 0 or b[former_cut-1] < b[i-1] - b[former_cut]:
		p, q = b[former_cut], b[i-1] - b[former_cut]
	else:
		p, q = b[former_cut-1], b[i-1] - b[former_cut-1]

	latter_cut = bisect(b[i:], (b[-1] + b[i-1]) / 2) + i
	#print(latter_cut)
	if b[latter_cut-1] - b[i-1] >= b[-1] - b[latter_cut]:
		r, s = b[latter_cut-1] - b[i-1], b[-1] - b[latter_cut-1]
	else:
		r, s = b[latter_cut] - b[i-1], b[-1] - b[latter_cut]
	#print(p, q, r, s)
	ans = min(ans, max(p, q, r, s) - min(p, q, r, s))

print(ans)

Fixed submission:
from bisect import bisect, bisect_left

n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
	b.append(b[-1] + a[i])

ans = float("inf")
for j in range(2, n-1):
	c = []
	f = bisect(b, b[j]/2)
	f -= 1
	if f == 0 or j == 2:
		c += [b[1]-b[0], b[j]-b[1]]
	elif f == j-1:
		c += [b[j-1]-b[0], b[j]-b[j-1]]
	else:
		if abs(b[f]-b[0] - (b[j]-b[f])) <= abs(b[f+1]-b[0] - (b[j]-b[f+1])):
			c += [b[f]-b[0], b[j]-b[f]]
		else:
			c += [b[f+1]-b[0], b[j]-b[f+1]]

	l = bisect(b, b[j]+((b[n]-b[j])/2))
	l -= 1
	#print(l)
	if l == j or j == n-2:
		c += [b[j+1]-b[j], b[n]-b[j+1]]
	elif l == n-1:
		c += [b[n-1]-b[j], b[n]-b[n-1]]
	else:
		if abs(b[l]-b[j] - (b[n]-b[l])) <= abs(b[l+1]-b[j] - (b[n]-b[l+1])):
			c += [b[l]-b[j], b[n]-b[l]]
		else:
			c += [b[l+1]-b[j], b[n]-b[l+1]]

	ans = min(ans, max(c)-min(c))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect

n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, n):
	b.append(b[-1] + a[i])
#print(b)

ans = float("inf")
for i in range(2, n - 1):
	former_cut = bisect(b[:i], b[i-1] / 2)
	if former_cut == 0 or b[former_cut-1] < b[i-1] - b[former_cut]:
		p, q = b[former_cut], b[i-1] - b[former_cut]
	else:
		p, q = b[former_cut-1], b[i-1] - b[former_cut-1]

	latter_cut = bisect(b, (b[-1] + b[i-1]) / 2)
	#print(latter_cut)
	if b[latter_cut-1] - b[i-1] >= b[-1] - b[latter_cut]:
		r, s = b[latter_cut-1] - b[i-1], b[-1] - b[latter_cut-1]
	else:
		r, s = b[latter_cut] - b[i-1], b[-1] - b[latter_cut]
	#print(p, q, r, s)
	ans = min(ans, max(p, q, r, s) - min(p, q, r, s))

print(ans)

Fixed submission:
from bisect import bisect, bisect_left

n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
	b.append(b[-1] + a[i])

ans = float("inf")
for j in range(2, n-1):
	c = []
	f = bisect(b, b[j]/2)
	f -= 1
	if f == 0 or j == 2:
		c += [b[1]-b[0], b[j]-b[1]]
	elif f == j-1:
		c += [b[j-1]-b[0], b[j]-b[j-1]]
	else:
		if abs(b[f]-b[0] - (b[j]-b[f])) <= abs(b[f+1]-b[0] - (b[j]-b[f+1])):
			c += [b[f]-b[0], b[j]-b[f]]
		else:
			c += [b[f+1]-b[0], b[j]-b[f+1]]

	l = bisect(b, b[j]+((b[n]-b[j])/2))
	l -= 1
	#print(l)
	if l == j or j == n-2:
		c += [b[j+1]-b[j], b[n]-b[j+1]]
	elif l == n-1:
		c += [b[n-1]-b[j], b[n]-b[n-1]]
	else:
		if abs(b[l]-b[j] - (b[n]-b[l])) <= abs(b[l+1]-b[j] - (b[n]-b[l+1])):
			c += [b[l]-b[j], b[n]-b[l]]
		else:
			c += [b[l+1]-b[j], b[n]-b[l+1]]

	ans = min(ans, max(c)-min(c))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect

n = int(input())
a = list(map(int, input().split()))
b = [a[0]]
for i in range(1, n):
	b.append(b[-1] + a[i])
#print(b)

ans = float("inf")
for i in range(2, n - 1):
	former_cut = bisect(b[:i], b[i-1] / 2)
	if former_cut == 0 or b[former_cut-1] < b[i-1] - b[former_cut]:
		p, q = b[former_cut], b[i-1] - b[former_cut]
	else:
		p, q = b[former_cut-1], b[i-1] - b[former_cut-1]

	latter_cut = bisect(b, (b[-1] + b[i-1]) / 2)
	#print(latter_cut)
	if b[latter_cut-1] - b[i-1] >= b[-1] - b[latter_cut]:
		r, s = b[latter_cut-1] - b[i-1], b[-1] - b[latter_cut-1]
	else:
		r, s = b[latter_cut] - b[i-1], b[-1] - b[latter_cut]
	#print(p, q, r, s)
	ans = min(ans, max(p, q, r, s) - min(p, q, r, s))

print(ans)

Fixed submission:
from bisect import bisect, bisect_left

n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
	b.append(b[-1] + a[i])

ans = float("inf")
for j in range(2, n-1):
	c = []
	f = bisect(b, b[j]/2)
	f -= 1
	if f == 0 or j == 2:
		c += [b[1]-b[0], b[j]-b[1]]
	elif f == j-1:
		c += [b[j-1]-b[0], b[j]-b[j-1]]
	else:
		if abs(b[f]-b[0] - (b[j]-b[f])) <= abs(b[f+1]-b[0] - (b[j]-b[f+1])):
			c += [b[f]-b[0], b[j]-b[f]]
		else:
			c += [b[f+1]-b[0], b[j]-b[f+1]]

	l = bisect(b, b[j]+((b[n]-b[j])/2))
	l -= 1
	#print(l)
	if l == j or j == n-2:
		c += [b[j+1]-b[j], b[n]-b[j+1]]
	elif l == n-1:
		c += [b[n-1]-b[j], b[n]-b[n-1]]
	else:
		if abs(b[l]-b[j] - (b[n]-b[l])) <= abs(b[l+1]-b[j] - (b[n]-b[l+1])):
			c += [b[l]-b[j], b[n]-b[l]]
		else:
			c += [b[l+1]-b[j], b[n]-b[l+1]]

	ans = min(ans, max(c)-min(c))

print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
b = [0 for _ in range(n + 1)]
for i in range(n):
	b[i + 1] = b[i] + a[i]
# b[i] = sum(a[:i])

suma = sum(a)
border = suma / 4

def func1(start_index, bit):
	# bit: 0 -> border を超えない, 1 -> border を超える
	end_index = start_index + 1
	
	if bit == 0:
		while b[end_index] - b[start_index] < border:
			end_index += 1
		if end_index == start_index + 1:
			return end_index, b[end_index] - b[start_index]
		else:
			return end_index - 1, b[end_index - 1] - b[start_index]

	elif bit == 1:
		while b[end_index] - b[start_index] < border:
			end_index += 1
		return end_index, b[end_index] - b[start_index]

def func2(bits):
	# bits: 0~7
	starts = [0]
	sums = []

	for i in range(3):
		f = func1(starts[-1], bits % 2)
		bits = bits // 2
		starts.append(f[0])
		sums.append(f[1])

	sums.append(b[n] - b[f[0]])

	return max(sums) - min(sums)

candidates = []

for i in range(8):
	candidates.append(func2(i))

print(min(candidates))

Fixed submission:
from bisect import bisect, bisect_left

n = int(input())
a = list(map(int, input().split()))
b = [0]
for i in range(n):
	b.append(b[-1] + a[i])

ans = float("inf")
for j in range(2, n-1):
	c = []
	f = bisect(b, b[j]/2)
	f -= 1
	if f == 0 or j == 2:
		c += [b[1]-b[0], b[j]-b[1]]
	elif f == j-1:
		c += [b[j-1]-b[0], b[j]-b[j-1]]
	else:
		if abs(b[f]-b[0] - (b[j]-b[f])) <= abs(b[f+1]-b[0] - (b[j]-b[f+1])):
			c += [b[f]-b[0], b[j]-b[f]]
		else:
			c += [b[f+1]-b[0], b[j]-b[f+1]]

	l = bisect(b, b[j]+((b[n]-b[j])/2))
	l -= 1
	#print(l)
	if l == j or j == n-2:
		c += [b[j+1]-b[j], b[n]-b[j+1]]
	elif l == n-1:
		c += [b[n-1]-b[j], b[n]-b[n-1]]
	else:
		if abs(b[l]-b[j] - (b[n]-b[l])) <= abs(b[l+1]-b[j] - (b[n]-b[l+1])):
			c += [b[l]-b[j], b[n]-b[l]]
		else:
			c += [b[l+1]-b[j], b[n]-b[l+1]]

	ans = min(ans, max(c)-min(c))

print(ans)