Problem p03112 (python)

Fixed submission:
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[aaa]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[a-1]),abs(cc - bb[0])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1],2*bb[bbb] - cc - aa[aaa-1],bb[bbb] + cc - 2 * aa[aaa-1])
		print(aaaa)

Buggy submission (verdict: Runtime Error):
import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])+min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])+min(abs(cc-aa[a]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[a-1]),abs(cc - bb[0])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1],2*bb[bbb] - cc - aa[aaa-1],bb[bbb] + cc - 2 * aa[aaa-1])
		print(aaaa)

Buggy submission (verdict: Runtime Error):

import bisect
a,b,c = map(int,input().split())

aa = []
bb = []
for i in range(a):
	aa.append(int(input()))
for i in range(b):
	bb.append(int(input()))
for i in range(c):
	cc = int(input())
	aaa = bisect.bisect_left(aa,cc)
	bbb = bisect.bisect_left(bb,cc)
	if aaa == 0 and bbb == 0:
		print(max(aa[0],bb[0]) - cc)
	elif aaa == a and bbb == b:
		print(cc - min(aa[a-1],bb[b-1]))
	elif aaa == 0:
		if bbb == b:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + aa[0] - bb[bbb-1])
		else:
			aaaa = min(abs(bb[bbb] - aa[0])min(abs(cc-aa[0]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[0])min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
			print(aaaa)
	elif bbb == 0:
		if aaa == a:
			print(min(abs(cc - aa[0]),abs(cc - bb[bbb-1])) + bb[0] - aa[aaa-1])
		else:
			bbbb = min(abs(bb[0] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[0])),abs(bb[0] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[0])))
			print(bbbb)
	elif aaa == a:
		aaaa = min(abs(bb[bbb] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb])),abs(bb[bbb-1] - aa[a-1])+min(abs(cc-aa[a-1]),abs(cc-bb[bbb-1])))
		print(aaaa)
	elif bbb == b:
		bbbb = min(abs(bb[b-1] - aa[aaa-1])+min(abs(cc-aa[aaa-1]),abs(cc-bb[b-1])),abs(bb[b-1] - aa[aaa])+min(abs(cc-aa[aaa]),abs(cc-bb[b-1])))
		print(bbbb)
	else:
		aaaa = min(max(aa[aaa],bb[bbb])-cc,cc-min(aa[aaa-1],bb[bbb-1]),2*aa[aaa] - cc - bb[bbb-1],aa[aaa] + cc - 2 * bb[bbb-1])
		print(aaaa)