Problem p03490 (python)

Fixed submission:
S = list(input())
N = len(S)
x,y = list(map(int,input().split()))

now = 1
YOKO = []
TATE = []
cnt=0
for i in range(N):
    if now==1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                YOKO.append(cnt)
            cnt=0
            now=-1*now
    elif now==-1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                TATE.append(cnt)
            cnt=0
            now=-1*now
if cnt>0:
    if now==1:
        YOKO.append(cnt)
    if now==-1:
        TATE.append(cnt)

NYOKO = len(YOKO)
NTATE = len(TATE)

dpYOKO = [0]*16001
dpTATE = [0]*16001
dpYOKO[0+8000]=1
dpTATE[0+8000]=1
if S[0]=="F":
    YOKOd = YOKO[1-1]
    newdp1 = [0]*16001
    for j in range(0,16001):
        if 0<=j-YOKOd:
            newdp1[j] = dpYOKO[j-YOKOd]
    dpYOKO = newdp1

    for i in range(2,NYOKO+1):
        YOKOd = YOKO[i-1]
        newdp1 = [0]*16001
        for j in range(0,16001):
            if (0<=j-YOKOd)and(j+YOKOd<=16000):
                newdp1[j] = max(dpYOKO[j-YOKOd],dpYOKO[j+YOKOd])
            elif (0<=j-YOKOd):
                newdp1[j] = dpYOKO[j-YOKOd]
            elif (j+YOKOd<=16000):
                newdp1[j] = dpYOKO[j+YOKOd]
        dpYOKO = newdp1
else:
    for i in range(1,NYOKO+1):
        YOKOd = YOKO[i-1]
        newdp1 = [0]*16001
        for j in range(0,16001):
            if (0<=j-YOKOd)and(j+YOKOd<=16000):
                newdp1[j] = max(dpYOKO[j-YOKOd],dpYOKO[j+YOKOd])
            elif (0<=j-YOKOd):
                newdp1[j] = dpYOKO[j-YOKOd]
            elif (j+YOKOd<=16000):
                newdp1[j] = dpYOKO[j+YOKOd]
        dpYOKO = newdp1

for i in range(1,NTATE+1):
    TATEd = TATE[i-1]
    newdp2 = [0]*16001
    for j in range(0,16001):
        if (0<=j-TATEd)and(j+TATEd<=16000):
            newdp2[j] = max(dpTATE[j-TATEd],dpTATE[j+TATEd])
        elif (0<=j-TATEd):
            newdp2[j] = dpTATE[j-TATEd]
        elif (j+TATEd<=16000):
            newdp2[j] = dpTATE[j+TATEd]
    dpTATE = newdp2

if dpYOKO[x+8000]==1 and dpTATE[y+8000]==1:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
S = list(input())
N = len(S)
x,y = list(map(int,input().split()))

now = 1
YOKO = []
TATE = []
cnt=0
for i in range(N):
    if now==1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                YOKO.append(cnt)
            cnt=0
            now=-1*now
    elif now==-1:
        if S[i]=="F":
            cnt+=1
        elif S[i]=="T":
            if cnt!=0:
                TATE.append(cnt)
            cnt=0
            now=-1*now
if cnt>0:
    if now==1:
        YOKO.append(cnt)
    if now==-1:
        TATE.append(cnt)

NYOKO = len(YOKO)
NTATE = len(TATE)

dpYOKO = [0]*16001
dpTATE = [0]*16001
dpYOKO[0+8000]=1
dpTATE[0+8000]=1

for i in range(1,NYOKO+1):
    YOKOd = YOKO[i-1]
    newdp1 = [0]*16001
    for j in range(0,16001):
        if (0<=j-YOKOd)and(j+YOKOd<=16000):
            newdp1[j] = max(dpYOKO[j-YOKOd],dpYOKO[j+YOKOd])
        elif (0<=j-YOKOd):
            newdp1[j] = dpYOKO[j-YOKOd]
        elif (j+YOKOd<=16000):
            newdp1[j] = dpYOKO[j+YOKOd]
    dpYOKO = newdp1

for i in range(1,NTATE+1):
    TATEd = TATE[i-1]
    newdp2 = [0]*16001
    for j in range(0,16001):
        if (0<=j-TATEd)and(j+TATEd<=16000):
            newdp2[j] = max(dpTATE[j-TATEd],dpTATE[j+TATEd])
        elif (0<=j-YOKOd):
            newdp2[j] = dpTATE[j-TATEd]
        elif (j+TATEd<=16000):
            newdp2[j] = dpTATE[j+TATEd]
    dpTATE = newdp2

if dpYOKO[x+8000]==1 and dpTATE[y+8000]==1:
    print("Yes")
else:
    print("No")