Problem p03141 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10000000)

N = int(input())
A = {}
B = {}
AplusB =[]

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	AplusB.append((a+b,i))

AplusB.sort()
AplusB.reverse()


takahashi = 0
aoki = 0

for j in range(N):
	if(j%2 == 0):
		takahashi += A[AplusB[j][1]]
	else:
		aoki += B[AplusB[j][1]]

print(takahashi-aoki)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)

N = int(input())
A = {}
B = {}
ABsa = {}
BAsa = {}
noneqqs = []
cnt = 0
aeqq_cnt = 0
beqq_cnt = 0

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	ABsa[i] = a-b
	BAsa[i] = b-a
	if(a==b):
		cnt += 1
	elif(a!=b):
		noneqqs.append(i)
	if(A[0]==a):
		aeqq_cnt += 1
	if(B[0]==b):
		beqq_cnt += 1

takahashi = 0
aoki = 0
if(cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]

		elif(j%2 == 1):
			maxkey2 = max(B,key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(aeqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(B,key=B.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(B, key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(beqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(A, key=A.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

else:
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(BAsa,key=BAsa.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(ABsa, key=ABsa.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000000000000000)

N = int(input())
A = {}
B = {}
ABsa = {}
BAsa = {}
noneqqs = []
cnt = 0
aeqq_cnt = 0
beqq_cnt = 0

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	ABsa[i] = a-b
	BAsa[i] = b-a
	if(a==b):
		cnt += 1
	elif(a!=b):
		noneqqs.append(i)
	if(A[0]==a):
		aeqq_cnt += 1
	if(B[0]==b):
		beqq_cnt += 1

takahashi = 0
aoki = 0
if(cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]

		elif(j%2 == 1):
			maxkey2 = max(B,key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(aeqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(B,key=B.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(B, key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(beqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(A, key=A.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

else:
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(BAsa,key=BAsa.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(ABsa, key=ABsa.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10000000)

N = int(input())
A = {}
B = {}
ABsa = {}
BAsa = {}
noneqqs = []
cnt = 0
aeqq_cnt = 0
beqq_cnt = 0

for i in range(N):
	a,b = map(int,input().split())
	A[i] = a
	B[i] = b
	ABsa[i] = a-b
	BAsa[i] = b-a
	if(a==b):
		cnt += 1
	elif(a!=b):
		noneqqs.append(i)
	if(A[0]==a):
		aeqq_cnt += 1
	if(B[0]==b):
		beqq_cnt += 1

takahashi = 0
aoki = 0
if(cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]

		elif(j%2 == 1):
			maxkey2 = max(B,key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(aeqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(B,key=B.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(B, key=B.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

elif(beqq_cnt == N):
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(A,key=A.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(A, key=A.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)

else:
	for j in range(N):
		if(j%2 == 0):
			maxkey = max(BAsa,key=BAsa.get)
			takahashi += int(A[maxkey])
			del A[maxkey]
			del B[maxkey]
		elif(j%2 == 1):
			maxkey2 = max(ABsa, key=ABsa.get)
			aoki += int(B[maxkey2])
			del B[maxkey2]
			del A[maxkey2]
	print(takahashi-aoki)