Problem p03488 (python)

Fixed submission:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
Xans,Yans = inpl()

Xmove = []
Ymove = []

First = True
flag = True
tmp = 0
for s in S+'T':
	if s == 'F':
		tmp += 1
	else:
		if First:
			Xans -= tmp
			First = False
		else:
			if flag:
				Xmove.append(tmp)
			else:
				Ymove.append(tmp)
		tmp = 0
		flag = not flag


Xsum = sum(Xmove)
if (Xsum+Xans)%2 == 1:
	print('No')
	sys.exit()
elif abs(Xans) > Xsum:
	print('No')
	sys.exit()
elif len(Xmove) == 0:
	if Xans != 0:
		print('No')
		sys.exit()
else:
	Xpos = (Xsum+Xans)//2
	dp = [0]*(Xpos+1)
	dp[0] = 1
	for x in Xmove:
		for i in range(Xpos,x-1,-1):
			dp[i] = dp[i-x]
		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

Ysum = sum(Ymove)
if (Ysum+Yans)%2 == 1:
	print('No')
	sys.exit()
elif abs(Yans) > Ysum:
	print('No')
	sys.exit()
elif len(Ymove) == 0:
	if Yans != 0:
		print('No')
		sys.exit()
else:
	Ypos = (Ysum+Yans)//2
	dp = [0]*(Ypos+1)
	dp[0] = 1
	for y in Ymove:
		for i in range(Ypos,y-1,-1):
			dp[i] = dp[i-y]
		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

print('Yes')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
Xans,Yans = inpl()

Xmove = []
Ymove = []

flag = True
tmp = 0
for s in S+'T':
	if s == 'F':
		tmp += 1
	else:
		if flag:
			Xmove.append(tmp)
		else:
			Ymove.append(tmp)
		tmp = 0
		flag = not flag

if len(Xmove) == 0:
	if Xans != 0:
		print('No')
		sys.exit()
else:
	Xsum = sum(Xmove)
	if (Xsum+Xans)%2 == 1:
		print('No')
		sys.exit()
	else:
		Xpos = (Xsum+Xans)//2
		dp = [0]*(Xpos+1)
		dp[0] = 1
		for x in Xmove:
			for i in range(Xpos,x-1,-1):
				dp[i] = dp[i-x]
			if dp[-1] == 1:
				break
		else:
			print('No')
			sys.exit()


if len(Ymove) == 0:
	if Yans != 0:
		print('No')
		sys.exit()
else:
	Ysum = sum(Ymove)
	if (Ysum+Yans)%2 == 1:
		print('No')
		sys.exit()
	else:
		Ypos = (Ysum+Yans)//2
		dp = [0]*(Ypos+1)
		dp[0] = 1
		for y in Ymove:
			for i in range(Ypos,y-1,-1):
				dp[i] = dp[i-y]

			if dp[-1] == 1:
				break
		else:
			print('No')
			sys.exit()

print('Yes')

Buggy submission (verdict: Runtime Error):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
Xans,Yans = inpl()

Xmove = []
Ymove = []

flag = True
tmp = 0
for s in S+'T':
	if s == 'F':
		tmp += 1
	else:
		if flag:
			Xmove.append(tmp)
		else:
			Ymove.append(tmp)
		tmp = 0
		flag = not flag


Xsum = sum(Xmove)

if (Xsum+Xans)%2 == 1:
	print('No')
	sys.exit()
else:
	Xpos = (Xsum+Xans)//2
	dp = [0]*(Xpos+1)
	dp[0] = 1
	for x in Xmove:
		for i in range(Xpos,x-1,-1):
			dp[i] = dp[i-x]

		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

Ysum = sum(Ymove)
if (Ysum+Yans)%2 == 1:
	print('No')
	sys.exit()
else:
	Ypos = (Ysum+Yans)//2
	dp = [0]*(Ypos+1)
	dp[0] = 1
	for y in Ymove:
		for i in range(Ypos,y-1,-1):
			dp[i] = dp[i-y]

		if dp[-1] == 1:
			break
	else:
		print('No')
		sys.exit()

print('Yes')