Problem:


Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No



Sample input:
FTFFTFFF
4 2


Sample output:
Yes

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
ss=[len(i) for i in s.split("T")]
x-=ss[0]
a=[]
b=[]
for i in range(1,len(ss)):
  if ss[i]==0:continue
  if i%2==1:b.append(ss[i])
  else:a.append(ss[i])
x=abs(x)
y=abs(y)
a.sort()
b.sort()
def check(n,a):
  dp=[[0]*20001 for _ in range(len(a)+1)]
  n+=10000
  dp[0][10000]=1
  for i in range(1,len(a)+1):
    for j in range(20001):
      if j+a[i-1]<=20000:dp[i][j+a[i-1]]|=dp[i-1][j]
      if j-a[i-1]>=0:dp[i][j-a[i-1]]|=dp[i-1][j]
  if dp[len(a)][n]==1:return True
  return False
if check(x,a) and check(y,b):print("Yes")
else:print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
ss=[len(i) for i in s.split("T")]
x-=ss[0]
a=[]
b=[]
for i in range(1,len(ss)):
  if ss[i]==0:continue
  if i%2==1:b.append(ss[i])
  else:a.append(ss[i])
x=abs(x)
y=abs(y)
a.sort()
b.sort()
def check(n,a):
  dp=[[0]*16001 for _ in range(len(a)+1)]
  n+=8000
  dp[0][8000]=1
  for i in range(1,len(a)+1):
    for j in range(16001):
      if j+a[i-1]<=16000:dp[i][j+a[i-1]]|=dp[i-1][j]
      if j-a[i-1]>=0:dp[i][j-a[i-1]]|=dp[i-1][j]
  if dp[len(a)][n]==1:return True
  return False
if check(x,a) and check(y,b):print("Yes")
else:print("No")

Submission status: Runtime Error

Solution code:
import copy
s = input()
x,y = map(int,input().split())

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)

num = 5
        
ver_li = [[0 for i in range(2)] for j in range(num*2+1)]
hor_li = [[0 for i in range(2)] for j in range(num*2+1)]

hor_li[num+ini][0] = 1
#print(sum(hor))
#exit()

for i in range(len(hor)):
    #c_hor = copy.deepcopy(hor_li)
    for j in range(num*2+1):
        if hor_li[j][0] == 1:
            #print(j,hor[i],i)
            hor_li[j][1] = 0
            hor_li[j+hor[i]][1] = 1
            hor_li[j-hor[i]][1] = 1
    for j in range(num*2+1):
        hor_li[j][0] = hor_li[j][1]

ver_li[num][0] = 1
for i in range(len(ver)):
    #c_ver = copy.deepcopy(ver_li)
    for j in range(num*2+1):
        if ver_li[j][0] == 1:
            ver_li[j][1] = 0
            #print(hor[i])
            ver_li[j+ver[i]][1] = 1
            ver_li[j-ver[i]][1] = 1
    for j in range(num*2+1):
        ver_li[j][0] = ver_li[j][1]
    
if hor_li[num+x][0] == 1 and ver_li[num+y][0] == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import copy
s = input()
x,y = map(int,input().split())

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)
        
ver_li = [0]*(8000*2+1)
hor_li = [0]*(8000*2+1)

hor_li[8000+ini] = 1
c_hor = copy.deepcopy(hor_li)
for i in range(len(hor)):
    #c_hor = copy.deepcopy(hor_li)
    for j in range(8000*2+1):
        if hor_li[j] == 1:
            #print(hor[i])
            c_hor[hor[i]] = 0
            c_hor[j+hor[i]] = 1
            c_hor[j-hor[i]] = 1
    hor_li = c_hor

ver_li[8000] = 1
c_ver = copy.deepcopy(ver_li)
for i in range(len(ver)):
    #c_ver = copy.deepcopy(ver_li)
    for j in range(8000*2+1):
        if ver_li[j] == 1:
            c_ver[ver[i]] = 0
            #print(hor[i])
            c_ver[j+ver[i]] = 1
            c_ver[j-ver[i]] = 1
    ver_li = c_ver
    
if hor_li[8000+x] == 1 and ver_li[8000+y] == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import copy
s = input()
x,y = map(int,input().split())

ver = []
hor = []
ini = 0
flag = 'h1'
tmp = 0

for i in range(len(s)):
    if s[i] == 'T':
        if flag == 'h':
            if tmp > 0:
                hor.append(tmp)
            tmp = 0
            flag = 'v'
        elif flag == 'v':
            if tmp > 0:
                ver.append(tmp)
            flag = 'h'
            tmp = 0
        elif flag == 'h1':
            if tmp > 0:
                ini = tmp
            flag = 'v'
            tmp = 0
    else:
        tmp += 1

if tmp > 0:
    if flag == 'h1':
        ini = tmp
    elif flag == 'h':
        hor.append(tmp)
    elif flag == 'v':
        ver.append(tmp)
        
ver_li = [0]*(8000*2+1)
hor_li = [0]*(8000*2+1)

hor_li[8000+1+ini] = 1

for i in range(len(hor)):
    c_hor = copy.deepcopy(hor_li)
    for j in range(8000*2+1):
        if hor_li[j] == 1:
            #print(hor[i])
            c_hor[j+hor[i]] = 1
            c_hor[j-hor[i]] = 1
    hor_li = c_hor

ver_li[8000+1] = 1

for i in range(len(ver)):
    c_ver = copy.deepcopy(ver_li)
    for j in range(8000*2+1):
        if ver_li[j] == 1:
            #print(hor[i])
            c_ver[j+ver[i]] = 1
            c_ver[j-ver[i]] = 1
    ver_li = c_ver
    
if hor_li[8000+1+x] == 1 and ver_li[8000+1+y] == 1:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

if order[0]=='T':
    x_start = 0
else:
    x_start = Xs[0]
    del Xs[0]

def dp(start, lis, goal):
    M = sum(lis)
    length = len(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M+start] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2+1):
            if dp[i-1][j]==1:
                if not j+x>=M*2+1:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(x_start, Xs, X) and dp(0, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())
 
Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        if not cnt==0:
            now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
if not cnt==0:
    now_lis.append(cnt)

def dp(length, lis, goal, isX):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2+1 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2+1):
                    if not isX and i!=1:
                        dp[i][j-x] = 1
    if goal+M>=M*2+1 or goal+M<0:
        return 0
    return dp[-1][goal+M]
 
if dp(lx, Xs, X, 1) and dp(ly, Ys, Y, 1):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not (j+x>=M*2 or j+x<0):
                    dp[i][j+x] = 1
                if not (j-x<0 or j-x>=M*2):
                    dp[i][j-x] = 1
    return dp[-1][goal+M]

if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    M = sum(lis)
    dp = [[0]*(M*2+1) for _ in range(length+1)]
    dp[0][M] = 1
    for i, x in enumerate(lis, 1):
        for j in range(M*2):
            if dp[i-1][j]==1:
                if not j+x>=M*2 or not j+x<0:
                    dp[i][j+x] = 1
                if not j-x<0 or not j-x>=M*2:
                    dp[i][j-x] = 1
    return dp[-1][goal+M]

if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    dp = [[0]*16000 for _ in range(length+1)]
    dp[0][8000] = 1
    for i, x in enumerate(lis, 1):
        for j in range(16000):
            if dp[i-1][j]==1:
                if not j+x>=16000 or not j+x<0:
                    dp[i][j+x] = 1
                if not j-x<0 or not j-x>=16000:
                    dp[i][j-x] = 1
    return dp[-1][goal+8000]

if dp(lx, Xs, X) and dp(ly, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)

def dp(length, lis, goal):
    dp = [[0]*16000 for _ in range(length+1)]
    dp[0][8000] = 1
    for i, x in enumerate(lis, 1):
        for j in range(16000):
            if dp[i-1][j]==1:
                if not j+x>=16000 or not j+x<0:
                    dp[i][j+x] = 1
                if not j-x<0 or not j-x>=16000:
                    dp[i][j-x] = 1
    return dp[-1][goal+8000]

if dp(xdp, Xs, X) and dp(ydp, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
xdp = [[0]*16000 for _ in range(lx+1)]
ydp = [[0]*16000 for _ in range(ly+1)]
xdp[0][8000] = 1
ydp[0][8000] = 1

def dp(dp, lis, goal):
    for i, x in enumerate(lis, 1):
        for j in range(16000):
            if dp[i-1][j]==1:
                if not j+x>=16000:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    return dp[-1][goal+8000]

if dp(xdp, Xs, X) and dp(ydp, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
order = list(input())
X, Y = map(int, input().split())

Xs = []
Ys = []
now_lis = Xs
cnt = 0
for s in order:
    if s=='T':
        now_lis.append(cnt)
        now_lis = Ys if now_lis==Xs else Xs
        cnt = 0
    else:
        cnt += 1
now_lis.append(cnt)
lx = len(Xs)
ly = len(Ys)
xdp = [[0]*8000 for _ in range(lx+1)]
ydp = [[0]*8000 for _ in range(ly+1)]
xdp[0][4000] = 1
ydp[0][4000] = 1

def dp(dp, lis, goal):
    for i, x in enumerate(lis, 1):
        for j in range(8000):
            if dp[i-1][j]==1:
                if not j+x>=8000:
                    dp[i][j+x] = 1
                if not j-x<0:
                    dp[i][j-x] = 1
    return dp[-1][goal+4000]

if dp(xdp, Xs, X) and dp(ydp, Ys, Y):
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
def main():
    *CMDs, = map(len, input().split('T'))
    CMDs += [0, 0, 0]  # 番兵,sliceでREを出さないため
    X, Y = map(int, input().split())
    X -= CMDs[0]  # この操作だけは対称性がない

    def is_reachable(cmds, goal):
        SFT = 8000
        # [-SFT,SFT] -> [0,SFT*2]
        dp = 1 << SFT
        for d in cmds:
            dp = (dp >> d) | (dp << d)
        return (dp >> (SFT + goal)) & 1

    cond = is_reachable(CMDs[2::2], X) and is_reachable(CMDs[1::2], Y)
    print('Yes' if cond else 'No')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    *CMDs, = map(len, input().split('T'))
    CMDs += [0, 0]  # 番兵,sliceでREを出さないため
    X, Y = map(int, input().split())
    X -= CMDs[0]  # この操作だけは対称性がない

    def is_reachable(cmds, goal):
        SFT = 8000
        # [-SFT,SFT] -> [0,SFT*2]
        dp = 1 << SFT
        for d in cmds:
            dp = (dp >> d) | (dp << d)
        return (dp >> (SFT + goal)) & 1

    cond = is_reachable(CMDs[2::2], X) and is_reachable(CMDs[1::2], Y)
    print('Yes' if cond else 'No')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    *CMDs, = map(len, input().split('T'))
    X, Y = map(int, input().split())
    X -= CMDs[0]  # この操作だけは対称性がない

    def is_reachable(cmds, goal):
        SFT = 8000
        # [-SFT,SFT] -> [0,SFT*2]
        dp = 1 << SFT
        for d in cmds:
            dp = (dp >> d) | (dp << d)
        return (dp >> (SFT + goal)) & 1

    cond = is_reachable(CMDs[2::2], X) and is_reachable(CMDs[1::2], Y)
    print('Yes' if cond else 'No')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from itertools import groupby

s = list(input())
x,y = map(int,input().split())

li = []

for key, value in groupby(s):
    li.append((key,len(list(value))))

x_li = []
y_li = []

now = 0
for i in li:
    if i[0] == "F":
        if now%2 == 0:
            x_li.append(i[1])
        else:
            y_li.append(i[1])
    else:
        now += i[1]

x_flag = False
y_flag = False

def possible(n,now,final,li):
    global x_flag,y_flag
    if n == len(li):
        if now == final:
            if li == x_li:
                x_flag = True
            else:
                y_flag = True
    else:
        return possible(n+1,now-li[n],final,li),possible(n+1,now+li[n],final,li)

possible(0,0,x,x_li)
possible(0,0,y,y_li)

if x_flag and y_flag:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
s = input()
x, y = map(int, input().split())
if s[0] == "F":
    x_choku = True
else:
    x_choku = False
# susumu = s.count("F")
# if susumu % 2 != (x + y) % 2:
#     print("No")
#     exit()
# if susumu < (x + y):
#     print("No")
#     exit()
x_houkou = True
cnt = 0
X = []
Y = []
for w in s:
    if w == "F":
        cnt += 1
    else:
        if x_houkou:
            X.append(cnt)
            cnt = 0
            x_houkou = False
        else:
            Y.append(cnt)
            cnt = 0
            x_houkou = True
if cnt > 0:
    if x_houkou:
        X.append(cnt)
    else:
        Y.append(cnt)
if x_choku:
    x -= X[0]
    X = X[1::]


x_ok = False
y_ok = False
if not X:
    if x == 0:
        x_ok = True
    else:
        print("No")
        exit()
if not Y:
    if y == 0:
        y_ok = True
    else:
        print("No")
        exit()

x = abs(x)
y = abs(y)
x_num = len(X)
y_num = len(Y)
if not x_ok:
    dp1 = [0] * (8000 + 1)
    dp2 = [0] * (8000 + 1)
    dp1[0] = 1
    for j in range(x_num):
        if j % 2 == 0:
            t = X[j]
            for i in range(8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = Y[j]
            for i in range(8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if x_num % 2 == 1:
        x_ok += dp2[x]
    else:
        x_ok += dp1[x]

if not y_ok:
    dp1 = [0] * (8000 + 1)
    dp2 = [0] * (8000 + 1)
    dp1[0] = 1
    for j in range(y_num):
        if j % 2 == 0:
            t = Y[j]
            for i in range(8000, -1, -1):
                if dp1[i] == 1:
                    if i + t <= 8000:
                        dp2[i + t] = 1
                    if i - t >= 0:
                        dp2[i + t] = 1
        else:
            t = Y[j]
            for i in range(8000, -1, -1):
                if dp2[i] == 1:
                    if i + t <= 8000:
                        dp1[i + t] = 1
                    if i - t >= 0:
                        dp1[i + t] = 1
    if y_num % 2 == 1:
        y_ok += dp2[y]
    else:
        y_ok += dp1[y]

if x_ok and y_ok:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
# input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7


S = list(input())
x, y = map(int, input().split())

N = 8000

X = [0]
Y = [0]
flag = True
for s in S:
    if s == 'F':
        if flag:
            X[-1] += 1
        else:
            Y[-1] += 1
    else:
        if flag:
            if Y[-1] != 0:
                Y.append(0)
        else:
            if X[-1] != 0:
                X.append(0)
        flag = not flag

dp = [[0] * (2 * N) for _ in range(len(X))]
if S[0] == 'F':
    dp[0][X[0] + N] = 1
else:
    dp[0][X[0] + N] = 1
    dp[0][-X[0] + N] = 1

for i in range(1, len(X)):
    x_ = X[i]
    for j in range(2 * N):
        if j >= x_:
            if dp[i - 1][j - x_]:
                dp[i][j] = 1
        if j + x_ < 2 * N:
            if dp[i - 1][j + x_]:
                dp[i][j] = 1

# print (dp)
if dp[-1][x + N] == 0:
    print ('No')
    exit()

dp = [[0] * (2 * N) for _ in range(len(Y))]
dp[0][Y[0] + N] = 1
dp[0][-Y[0] + N] = 1

for i in range(1, len(Y)):
    y_ = Y[i]
    for j in range(2 * N):
        if j >= y_:
            if dp[i - 1][j - y_]:
                dp[i][j] = 1
        if j + y_ < 2 * N:
            if dp[i - 1][j + y_]:
                dp[i][j] = 1

if dp[-1][y + N] == 0:
    print ('No')
    exit()

print ('Yes')

Submission status: Runtime Error

Solution code:
def parse(s):
    first_val = None
    verticals, horizontals = list(), list()
    seq = 0
    is_horizontal = True
    is_first = True
    for c in s:
        if c == 'T':
            if is_first:
                first_val = seq
                is_first = False
            elif is_horizontal:
                horizontals.append(seq)
            else:
                verticals.append(seq)
            is_horizontal = not is_horizontal
            seq = 0
        else:
            seq += 1
    if seq > 0:
        if is_first:
            first_val = seq
        elif is_horizontal:
            horizontals.append(seq)
        else:
            verticals.append(seq)
    return first_val, verticals, horizontals


def check(vals, target):
    N = sum(vals)
    if N == 0:
        return target == 0
    dp = [0 for _ in range(N + 1)]
    dp[0] = 1
    for v in vals:
        for n in range(N + 1):
            if n + v <= N:
                dp[n + v] = max([dp[n], dp[n + v]])
            if n - v >= 0:
                dp[n - v] = max([dp[n], dp[n - v]])
            else:
                dp[v - n] = max([dp[n], dp[v - n]])
    return dp[abs(target)] == 1


def main():
    S = input()
    X, Y = list(map(int, input().split(' ')))
    fv, vs, hs = parse(S)
    if check(hs, X - fv) and check(vs, Y):
        print('Yes')
    else:
        print('No')


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
s = input()
n = len(s)
x, y = map(int, input().split())
dx = []
dy = []
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        xy += 1
        xy %= 2
        if xy:
            dx.append(cnt)
        else:
            dy.append(cnt)
        cnt = 0
if cnt != 0:
    if xy:
        dy.append(cnt)
    else:
        dx.append(cnt)
sum_x = sum(dx) + 2
sum_y = sum(dy) + 2
dpx = [[0] * sum_x for i in range(len(dx))]
dpy = [[0] * sum_y for i in range(len(dy))]

if dx:
    dpx[0][dx[0]] = True
if dy:
    dpy[0][dy[0]] = True
    dpy[0][-dy[0]] = True
for i in range(1, len(dx)):
    for j in range(0, sum_x):
        dpx[i][j] = dpx[i-1][j-dx[i]] or dpx[i-1][(j+dx[i])%sum_x]
for i in range(1, len(dy)):
    for j in range(0, sum_y):
        dpy[i][j] = dpy[i-1][j-dy[i]] or dpy[i-1][(j+dy[i])%sum_y]
print('Yes' if x < sum_x and dpx[-1][x] and y < sum_y and dpy[-1][y] else 'No')

Submission status: Runtime Error

Solution code:
s = input()
n = len(s)
x, y = map(int, input().split())
dx = []
dy = []
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        xy += 1
        xy %= 2
        if xy:
            dx.append(cnt)
        else:
            dy.append(cnt)
        cnt = 0
if cnt != 0:
    if xy:
        dy.append(cnt)
    else:
        dx.append(cnt)
sum_x = sum(dx) + 2
sum_y = sum(dy) + 2
dpx = [[0] * sum_x for i in range(len(dx))]
dpy = [[0] * sum_y for i in range(len(dy))]

if dx:
    dpx[0][dx[0]] = True
if dy:
    dpy[0][dy[0]] = True
    dpy[0][-dy[0]] = True
for i in range(1, len(dx)):
    for j in range(0, sum_x):
        dpx[i][j] = dpx[i-1][j-dx[i]] or dpx[i-1][(j+dx[i])%sum_x]
for i in range(1, len(dy)):
    for j in range(0, sum_y):
        dpy[i][j] = dpy[i-1][j-dy[i]] or dpy[i-1][(j+dy[i])%sum_y]
print('Yes' if dpx[-1][x] and dpy[-1][y] else 'No')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * (sum(hist_x) + 10)  # ここが0になるときがある
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    if -1 < x + shift < x_range:
        ansx = dpx[-1][x + shift]
    else:
        ansx = False


if hist_y:
    y_range = 2 * (sum(hist_y) + 10)  # ここが0になるときがある
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
    if -1 < y + shift < y_range:
        ansy = dpy[-1][y + shift]
    else:
        ansy = False
else:
    ansy = y == 0

print('Yes' if ansx and ansy else 'No')

# print(ansx, ansy)
# print(*dpy, sep='\n')

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc082/tasks/arc087_b


def run_length_encoding(s):
    '''
    連長圧縮を行う
    s ... iterable object e.g. list, str
    return
    ----------
    s_composed,s_num,s_idx
    それぞれ、圧縮後の文字列、その文字数、その文字が始まるidx
    '''
    s_composed = []
    s_sum = []
    s_idx = [0]
    pre = s[0]
    cnt = 1
    for i, ss in enumerate(s[1:], start=1):
        if pre == ss:
            cnt += 1
        else:
            s_sum.append(cnt)
            s_composed.append(pre)
            s_idx.append(i)
            cnt = 1
            pre = ss
    s_sum.append(cnt)
    s_composed.append(pre)
    # assert len(s_sum) == len(s_composed)
    return s_composed, s_sum, s_idx

# データに関しては事前にランレングス圧縮しておくと楽かもね


s = input()
x, y = map(int, input().split())
s_composed, s_sum, _ = run_length_encoding(s)

if s_composed[0] == 'T':  # 必ずTから始まるようにしたい
    s_composed.insert(0, 'F')
    s_sum.insert(0, 0)
# 前処理 xの移動履歴とyの移動履歴を作る
is_x = True
hist_x = []
hist_y = []
for s, n in zip(s_composed, s_sum):
    if s == 'T':
        if n & 1:  # xyの状態を反転させる
            is_x = not is_x
    else:
        if is_x:
            hist_x.append(n)
        else:
            hist_y.append(n)

'''
dpx[i][j]...x座標がjであることが可能か？ x方向の移動についてi回目の移動のあとに。
dpy[i][j]...y座標がjであることが可能か？ y方向の移動についてi回目の移動のあとに。

更新則
xについての移動,距離d移動するとすると
dpx[i+1][j+d]=dpx[i+1][j+d] or dpx[i][j]
dpx[i+1][j-d]=dpx[i+1][j-d] or dpx[i][j]
(配るDP)で更新すると楽かな？
yも同様

初期条件
dpx[:][:]=False
dpx[0][d_x0]=True #xの0回目の移動。

dpy[:][:] = False
dpy[0][d_y0]=True 簡単のために
dpy[0][-d_y0]=True
'''

if hist_x:
    x_range = 2 * (sum(hist_x) + 3000)  # ここが0になるときがある
    shift = x_range // 2
    dpx = [[False] * x_range for _ in range(len(hist_x))]
    dpx[0][hist_x[0] + shift] = True
    for i in range(1, len(hist_x)):
        d = hist_x[i]
        for j in range(x_range):
            if j + d < x_range:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j + d]
            if -1 < j - d:
                dpx[i][j] = dpx[i][j] or dpx[i - 1][j - d]
    ansx = dpx[-1][x + shift]
# else:
#     ansx = x == 0

if hist_y:
    y_range = 2 * (sum(hist_y) + 3000)  # ここが0になるときがある
    shift = y_range // 2
    dpy = [[False] * y_range for _ in range(len(hist_y))]
    dpy[0][hist_y[0] + shift] = True
    dpy[0][-hist_y[0] + shift] = True
    for i in range(1, len(hist_y)):
        d = hist_y[i]
        for j in range(y_range):
            if j + d < y_range:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j + d]
            if -1 < j - d:
                dpy[i][j] = dpy[i][j] or dpy[i - 1][j - d]
    ansy = dpy[-1][y + shift]
else:
    ansy = y == 0

print('Yes' if ansx and ansy else 'No')


# print(ansx, ansy)
# print(*dpy, sep='\n')