Problem p03312 (python)

Fixed submission:
import bisect

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	B = [0]
	for i in range(N):
		B.append(B[-1] + A[i])
	#print(B)
	ans = float("inf")
	for i in range(2, N - 1):
		Q = B[i]
		x = bisect.bisect_left(B, Q // 2)
		P = max(B[x], B[1])
		P = min(P, B[i - 1])
		if x >= 1:
			P2 = max(B[x - 1], B[1])
			Q2 = Q - P2
		Q3 = max(B[i] - B[i - 1], B[i] - B[x + 1])
		P3 = Q - Q3
		Q -= P

		S = B[N] - B[i]
		y = bisect.bisect_left(B, (B[i] + B[N]) // 2)
		R = max(B[y] - B[i] , B[i + 1] - B[i])
		R = min(R, B[N - 1] - B[i])
		R2 = max(B[y - 1] - B[i] , B[i + 1] - B[i])
		S2 = S - R2
		if y < N:
			S3 = max(B[N] - B[N - 1], B[N] - B[y + 1])
			R3 = S - S3
		S -= R
		ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
		ans = min(ans, max(P3, Q3, R, S) - min(P3, Q3, R, S))
		ans = min(ans, max(P, Q, R2, S2) - min(P, Q, R2, S2))
		ans = min(ans, max(P3, Q3, R2, S2) - min(P3, Q3, R2, S2))
		if x >= 1:
			ans = min(ans, max(P2, Q2, R, S) - min(P2, Q2, R, S))
			ans = min(ans, max(P2, Q2, R2, S2) - min(P2, Q2, R2, S2))
		if y < N:
			ans = min(ans, max(P, Q, R3, S3) - min(P, Q, R3, S3))
			ans = min(ans, max(P3, Q3, R3, S3) - min(P3, Q3, R3, S3))
		if x >= 1 and y < N:
			ans = min(ans, max(P2, Q2, R3, S3) - min(P2, Q2, R3, S3))
	print(ans)

if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
import bisect

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	B = [0]
	for i in range(N):
		B.append(B[-1] + A[i])
	#print(B)
	ans = float("inf")
	for i in range(2, N - 1):
		Q = B[i]
		x = bisect.bisect_left(B, Q // 2)
		P = max(B[x], B[1])
		P2 = max(B[x - 1], B[1])
		Q3 = max(B[i] - B[i - 1], B[i] - B[x + 1])
		P3 = Q - Q3
		Q2 = Q - P2
		Q -= P
		S = B[N] - B[i]
		y = bisect.bisect_left(B, (B[i] + B[N]) // 2)
		R = max(B[y] - B[i] , B[i + 1] - B[i])
		R2 = max(B[y - 1] - B[i] , B[i + 1] - B[i])
		S3 = max(B[N] - B[N - 1], B[N] - B[y + 1])
		R3 = S - S3
		S2 = S - R2
		S -= R
		#print(P, Q, R, S)
		#print(sum([P, Q, R, S]))
		ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
		ans = min(ans, max(P2, Q2, R2, S2) - min(P2, Q2, R2, S2))
		ans = min(ans, max(P3, Q3, R3, S3) - min(P3, Q3, R3, S3))
	print(ans)


if __name__ == '__main__':
	main()

Buggy submission (verdict: Runtime Error):
import bisect

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	A = getlist()
	B = [0]
	for i in range(N):
		B.append(B[-1] + A[i])
	#print(B)
	ans = float("inf")
	for i in range(2, N - 2):
		Q = B[i]
		x = bisect.bisect_left(B, Q // 2)
		P = max(B[x], B[1])
		P2 = max(B[x - 1], B[1])
		Q3 = max(B[i] - B[i - 1], B[i] - B[x + 1])
		P3 = Q - Q3
		Q2 = Q - P2
		Q -= P
		S = B[N] - B[i]
		y = bisect.bisect_left(B, (B[i] + B[N]) // 2)
		R = max(B[y] - B[i] , B[i + 1] - B[i])
		R2 = max(B[y - 1] - B[i] , B[i + 1] - B[i])
		S3 = max(B[N] - B[N - 1], B[N] - B[y + 1])
		R3 = S - S3
		S2 = S - R2
		S -= R
		#print(P, Q, R, S)
		#print(sum([P, Q, R, S]))
		ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
		ans = min(ans, max(P2, Q2, R2, S2) - min(P2, Q2, R2, S2))
		ans = min(ans, max(P3, Q3, R3, S3) - min(P3, Q3, R3, S3))
	print(ans)


if __name__ == '__main__':
	main()