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:
def main():
    s=input()
    x,y=map(int,input().split())

    lf = [0]
    tf = 0
    for i in range(len(s)):
        if s[i] =="F":
            lf[tf] += 1
        else:
            lf.append(0)
            tf += 1

    lx = [lf[i] for i in range(len(lf)) if i % 2 == 0]
    ly = [lf[i] for i in range(len(lf)) if i % 2 == 1]
    dpxcenter = sum(lx)
    dpxedge = dpxcenter*2+20
    dpx = [[False for _ in range(dpxedge)] for _ in range(len(lx)+1)]
    dpx[1][dpxcenter+lx[0]+10] = True
    for i in range(1,len(lx)):
        for j in range(dpxedge):
            if j-lx[i] >= 0:
                dpx[i+1][j] = dpx[i][j-lx[i]]
            if j+lx[i] < dpxedge:
                dpx[i+1][j] = dpx[i+1][j] or dpx[i][j+lx[i]]

    dpycenter = sum(ly)
    dpyedge = dpycenter*2+20
    dpy = [[False for _ in range(dpyedge)] for _ in range(len(ly)+1)]
    dpy[0][dpycenter+10] = True
    for i in range(len(ly)):
        for j in range(dpyedge):
            if j-ly[i] >= 0:
                dpy[i+1][j] = dpy[i][j-ly[i]]
            if j+ly[i] < dpyedge:
                dpy[i+1][j] = dpy[i+1][j] or dpy[i][j+ly[i]]

    if dpx[-1][x+dpxcenter+10] and dpy[-1][y+dpycenter+10]:return "Yes"
    else:return "No"

print(main())

Submission status: Runtime Error

Solution code:
def main():
    s=input()
    x,y=map(int,input().split())

    lf = [0]
    tf = 0
    for i in range(len(s)):
        if s[i] =="F":
            lf[tf] += 1
        else:
            lf.append(0)
            tf += 1

    lx = [lf[i] for i in range(len(lf)) if i % 2 == 0]
    ly = [lf[i] for i in range(len(lf)) if i % 2 == 1]
    dpxcenter = sum(lx)
    dpxedge = dpxcenter*2+10
    dpx = [[False for _ in range(dpxedge)] for _ in range(len(lx)+1)]
    dpx[1][dpxcenter+lx[0]+5] = True
    for i in range(1,len(lx)):
        for j in range(dpxedge):
            if j-lx[i] >= 0:
                dpx[i+1][j] = dpx[i][j-lx[i]]
            if j+lx[i] < dpxedge:
                dpx[i+1][j] = dpx[i+1][j] or dpx[i][j+lx[i]]

    dpycenter = sum(ly)
    dpyedge = dpycenter*2+10
    dpy = [[False for _ in range(dpyedge)] for _ in range(len(ly)+1)]
    dpy[0][dpycenter+5] = True
    for i in range(len(ly)):
        for j in range(dpyedge):
            if j-ly[i] >= 0:
                dpy[i+1][j] = dpy[i][j-ly[i]]
            if j+ly[i] < dpyedge:
                dpy[i+1][j] = dpy[i+1][j] or dpy[i][j+ly[i]]

    if dpx[-1][x+dpxcenter+5] and dpy[-1][y+dpycenter+5]:return "Yes"
    else:return "No"

print(main())

Submission status: Runtime Error

Solution code:
def main():
    s=input()
    x,y=map(int,input().split())

    lf = [0]
    tf = 0
    for i in range(len(s)):
        if s[i] =="F":
            lf[tf] += 1
        else:
            lf.append(0)
            tf += 1

    lx = [lf[i] for i in range(len(lf)) if i % 2 == 0]
    ly = [lf[i] for i in range(len(lf)) if i % 2 == 1]
    start =lx.pop(0)
    dpxcenter = sum(lx)
    dpxedge = (dpxcenter+start)*2+10
    dpx = [[False for _ in range(dpxedge)] for _ in range(len(lx)+1)]
    dpx[0][dpxcenter+start+5] = True
    for i in range(len(lx)):
        for j in range(dpxedge):
            if j-lx[i] >= 0:
                dpx[i+1][j] = dpx[i][j-lx[i]]
            if j+lx[i] < dpxedge:
                dpx[i+1][j] = dpx[i+1][j] or dpx[i][j+lx[i]]

    dpycenter = sum(ly)
    dpyedge = dpycenter*2+10
    dpy = [[False for _ in range(dpyedge)] for _ in range(len(ly)+1)]
    dpy[0][dpycenter+5] = True
    for i in range(len(ly)):
        for j in range(dpyedge):
            if j-ly[i] >= 0:
                dpy[i+1][j] = dpy[i][j-ly[i]]
            if j+ly[i] < dpyedge:
                dpy[i+1][j] = dpy[i+1][j] or dpy[i][j+ly[i]]

    if dpx[-1][x+dpxcenter+5] and dpy[-1][y+dpycenter+5]:return "Yes"
    else:return "No"

print(main())

Submission status: Runtime Error

Solution code:
#例外処理がお手上げ
s = input()
ls = len(s)
x,y = map(int, input().split( ))
cnt_fx = 0
FX = []
cnt_fy = 0
FY = []

direct_x = 1
for i in range(len(s)):
    if s[i]=="F":
        if direct_x:
            cnt_fx += 1
        else:
            cnt_fy += 1
    else:
        if direct_x:
            FX.append(cnt_fx)
            cnt_fx = 0
        else:
            FY.append(cnt_fy)
            cnt_fy = 0
        direct_x ^= 1
if s[-1] =="F":
    if direct_x:
        FX.append(cnt_fx)
    else:
        FY.append(cnt_fy)
#容量16000以下のナップサック
#反転した区間の和*2が2sum(CFX)-xになるか？(yも同様,)
x_sum = sum(FX)
y_sum = sum(FY)
lx = len(FX)
ly = len(FY)

if x<0:
    x *= -1
    dpx = [[0 for i in range(2*x_sum+1+x)] for i in range(ls)]
    x_sum -= 2*FX[0]
    FX.pop(0)
    dpx[0][0] = 1
    if FX:
      dpx[0][FX[0]] = 1
else:
    dpx = [[0 for i in range(2*x_sum+1+x)] for i in range(ls)]
    dpx[0][0] = 1
    dpx[0][FX[0]] = 1

if y<0:
    y *= -1
    dpy = [[0 for i in range(2*y_sum+1+y)] for i in range(ls)]
    dpy[0][0] = 1
    dpy[0][FY[0]] = 1
else:
    dpy = [[0 for i in range(2*y_sum+1+5*y)] for i in range(ls)]
    dpy[0][0] = 1
    dpy[0][FY[0]] = 1


for i in range(lx):
    for j in range(x):
        dpx[i][j] = max(dpx[i][j],dpx[i-1][j],dpx[i-1][j-2*FX[i]])


for i in range(ly):
    for j in range(y):
        dpy[i][j] = max(dpy[i][j],dpy[i-1][j],dpy[i-1][j-2*FY[i]])
print(FX,FY)
if dpx[lx-1][x_sum-x] and dpy[ly-1][y_sum-y]:
    print('Yes')
else:
    print("No")

Submission status: Runtime Error

Solution code:
s=input()
x,y=map(int,input().split())
xx,yy=[],[]
w=[0,0]
ans=chk=0
f=0
for i in s:
    if f==0 and i=='F':
        w[0]+=1
    elif f==0 and i=='T':
        f=1
    elif f==1 and i=='F':
        chk+=1
    elif f==1 and i=='T':
        yy.append(chk)
        chk=0
        f=2
    elif f==2 and i=='F':
        chk+=1
    elif f==2 and i=='T':
        xx.append(chk)
        chk=0
        f=1
if chk and f==1:
    yy.append(ckh)
elif chk and f==2:
    xx.append(chk)
xx.sort()
yy.sort()
for i in xx[::-1]:
    if w[0]<=x:
        w[0]+=i
    else:
        w[0]-=i
for i in yy[::-1]:
    if w[1]<=y:
        w[1]+=i
    else:
        w[1]-=i
print("Yes" if w[0]==x and w[1]==y else "No")

Submission status: Runtime Error

Solution code:
# D
s = input()
x, y = map(int, input().split())
finished = []

def dfs(now, index, direction):
    if index == len(s)-1:
        if s[index] == 'T':
            finished.append(now)
            return
        elif s[index] == 'F':
            if direction==1:
                nowx, nowy = now
                next = (nowx+1, nowy)
                finished.append(next)
                return
            elif direction==2:
                nowx, nowy = now
                next = (nowx, nowy+1)
                finished.append(next)
                return
            elif direction==3:
                nowx, nowy = now
                next = (nowx-1, nowy)
                finished.append(next)
                return
            elif direction==0:
                nowx, nowy = now
                next = (nowx, nowy-1)
                finished.append(next)
                return

    if s[index] == 'F':
        if direction==1:
            nowx, nowy = now
            next = (nowx+1, nowy)
            dfs(next, index+1, direction)

        elif direction==2:
            nowx, nowy = now
            next = (nowx, nowy+1)
            dfs(next, index+1, direction)

        elif direction==3:
            nowx, nowy = now
            next = (nowx-1, nowy)
            dfs(next, index+1, direction)

        elif direction==0:
            nowx, nowy = now
            next = (nowx, nowy-1)
            dfs(next, index+1, direction)

    elif s[index] == 'T':
        direction1 = (direction + 1)%4
        direction2 = (direction - 1)%4
        dfs(now, index+1, direction1)
        dfs(now, index+1, direction2)


now = (0, 0)
direction = 1
dfs(now, 0, direction)
if (x, y) in finished:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
s = input()
x, y = map(int, input().split())
finished = []

def dfs(now, index, direction):
    if index == len(s)-1:
        if s[index] == 'T':
            finished.append(now)
            return
        elif s[index] == 'F':
            if direction==1:
                nowx, nowy = now
                next = (nowx+1, nowy)
                finished.append(next)
                return
            elif direction==2:
                nowx, nowy = now
                next = (nowx, nowy+1)
                finished.append(next)
                return
            elif direction==3:
                nowx, nowy = now
                next = (nowx-1, nowy)
                finished.append(next)
                return
            elif direction==0:
                nowx, nowy = now
                next = (nowx, nowy-1)
                finished.append(next)
                return

    if s[index] == 'F':
        if direction==1:
            nowx, nowy = now
            next = (nowx+1, nowy)
            dfs(next, index+1, direction)

        elif direction==2:
            nowx, nowy = now
            next = (nowx, nowy+1)
            dfs(next, index+1, direction)

        elif direction==3:
            nowx, nowy = now
            next = (nowx-1, nowy)
            dfs(next, index+1, direction)

        elif direction==0:
            nowx, nowy = now
            next = (nowx, nowy-1)
            dfs(next, index+1, direction)

    elif s[index] == 'T':
        direction1 = (direction + 1)%4
        direction2 = (direction - 1)%4
        dfs(now, index+1, direction1)
        dfs(now, index+1, direction2)


now = (0, 0)
direction = 1
dfs(now, 0, direction)
print(finished)
if (x, y) in finished:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(z, zz, z_list):
  if len(z_list) == 0:
    if z != zz:
      return 0
    else:
      return 1
  else:
    z_n = len(z_list)
    z_hash = {}
    z_hash[0] = {}
    z_hash[0][zz] = 1
    z_hash[z_n] = {}
    z_hash[z_n][z] = 1
    for i in range(0,z_n):
      if i+1 not in z_hash:
        z_hash[i+1] = {}
        for b_z in z_hash[i]:
          a_z = b_z + z_list[i]
          z_hash[i+1][a_z] = 1
          a_z = b_z - z_list[i]
          z_hash[i+1][a_z] = 1
      else:
        for b_z in z_hash[i]:
          a_z = b_z + z_list[i]
          if a_z in z_hash[i+1]:
            return 1
          a_z = b_z - z_list[i]
          if a_z in z_hash[i+1]:
            return 1
        return 0
      if z_n-i-1 not in z_hash:
        z_hash[z_n-i-1] = {}
        for a_z in z_hash[z_n-i]:
          b_z = a_z + z_list[z_n-i]
          z_hash[z_n-i-1][b_z] = 1
          b_z = a_z - z_list[z_n-i]
          z_hash[z_n-i-1][b_z] = 1
      else:
        for a_z in z_hash[z_n-i]:
          b_z = a_z + z_list[z_n-i]
          if b_z in z_hash[z_n-i-1]:
            return 1
          b_z = a_z + z_list[z_n-i]
          if b_z in z_hash[z_n-i-1]:
            return 1
        return 0



s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if check(x, xx, x_list) == 1 and check(y, yy, y_list) == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(z, zz, z_list, dire):
  zz += z_list[0] * dire
  if len(z_list) == 1:
    if z == zz:
      return 1
    else:
      return 0
  else:
    if check(z, zz, z_list[1:], 1) == 0:
      if check(z, zz, z_list[1:], -1) == 0:
      	return 0
    return 1

    
def func(x, y, xx, yy, x_list, y_list):
  if len(x_list) == 0:
    if x != xx:
      return 0
  else:
    if check(x, xx, x_list, 1) == 0:
      if check(x, xx, x_list, -1) == 0:
      	return 0
  if len(y_list) == 0:
    if y != yy:
      return 0
  else:
    if check(y, yy, y_list, 1) == 0:
      if check(y, yy, y_list, -1) == 0:
      	return 0
  return 1

s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if func(x, y, xx, yy, x_list, y_list) == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(z, zz, z_list, dire):
  zz += z_list[0] * dire
  if len(z_list) == 1:
    if z == zz:
      return 1
    else:
      return 0
  else:
    if check(z, zz, z_list[1:], 1) == 0:
      return 0
    if check(z, zz, z_list[1:], -1) == 0:
      return 0
    return 1

    
def func(x, y, xx, yy, x_list, y_list):
  if len(x_list) == 0:
    if x != xx:
      return 0
  else:
    if check(x, xx, x_list, 1) == 0:
      return 0
    if check(x, xx, x_list, -1) == 0:
      return 0
  if len(y_list) == 0:
    if y != yy:
      return 0
  else:
    if check(y, yy, y_list, 1) == 0:
      return 0
    if check(y, yy, y_list, -1) == 0:
      return 0
  return 1

s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if func(x, y, xx, yy, x_list, y_list) == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(z, zz, z_list, dire):
  zz += z_list[0] * dire
  if len(z_list) == 1:
    if z == zz:
      return 1
    else:
      return 0
  else:
    if check(z, zz, z_list[1:], 1) == 0 and check(z, zz, z_list[1:], -1) == 0:
      return 0
    else:
      return 1

    
def func(x, y, xx, yy, x_list, y_list):
  if len(x_list) == 0:
    if x != xx:
      return 0
  else:
    if check(x, xx, x_list, 1) == 0 and check(x, xx, x_list, -1) == 0:
      return 0
  if len(y_list) == 0:
    if y != yy:
      return 0
  else:
    if check(y, yy, y_list, 1) == 0 and check(y, yy, y_list, -1) == 0:
      return 0
  return 1

s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if func(x, y, xx, yy, x_list, y_list) == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(z, zz, z_list, dire):
  zz += z_list[0] * dire
  if len(z_list) == 1:
    if z == zz:
      return 1
    else:
      return 0
  else:
    if check(z, zz, z_list[1:], 1) == 0 and check(z, zz, z_list[1:], 1) == 0:
      return 0
    else:
      return 1

    
def func(x, y, xx, yy, x_list, y_list):
  if len(x_list) == 0:
    if x != xx:
      return 0
  else:
    if check(x, xx, x_list, 1) == 0 and check(x, xx, x_list, -1) == 0:
      return 0
  if len(y_list) == 0:
    if y != yy:
      return 0
  else:
    if check(y, yy, y_list, 1) == 0 and check(y, yy, y_list, -1) == 0:
      return 0
  return 1

s = input()
x, y = map(int, input().split())
data = s.split("T")
xx, yy = 0, 0
i = 0
x_list = []
y_list = []
for Fs in data:
  if len(Fs) == 0:
    i += 1
  else:
    if i == 0:
      xx = len(Fs)
    elif i % 2 == 0:
      x_list.append(len(Fs))
    else:
      y_list.append(len(Fs))
    i += 1
if func(x, y, xx, yy, x_list, y_list) == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(s, x, y, xx, yy, dire):
  if len(s) > 0:
    i = 0
    for command in s:
      i += 1
      if command == "F":
        xx += dire[0]
        yy += dire[1]
      else:
        if dire[0] != 0:
          if check(s[i:], x, y, xx, yy, [0, 1]) == 1:
            return 1
          if check(s[i:], x, y, xx, yy, [0, -1]) == 1:
            return 1
        elif dire[1] != 0:
          if check(s[i:], x, y, xx, yy, [1, 0]) == 1:
            return 1
          if check(s[i:], x, y, xx, yy, [-1, 0]) == 1:
            return 1
        return 0
  if xx == x and yy == y:
    return 1
  return 0


s = input()
x, y = map(int, input().split())
xx, yy = 0, 0
dire = [1, 0] #右1左-1,上1下-1
res = check(s, x, y, xx, yy, dire)
if res == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(s, x, y, xx, yy, dire):
  if len(s) > 0:
    i = 0
    for command in s:
      i += 1
      if command == "F":
        xx += dire[0]
        yy += dire[1]
      else:
        if dire[0] != 0:
          if check(s[i:], x, y, xx, yy, [0, 1]) == 1:
            return 1
          if check(s[i:], x, y, xx, yy, [0, -1]) == 1:
            return 1
        elif dire[1] != 0:
          if check(s[i:], x, y, xx, yy, [1, 0]) == 1:
            return 1
          if check(s[i:], x, y, xx, yy, [-1, 0]) == 1:
            return 1
        return 0
  if xx == x and yy == y:
    return 1
  else:
    return 0


s = input()
x, y = map(int, input().split())
xx, yy = 0, 0
dire = [1, 0] #右1左-1,上1下-1
res = check(s, x, y, xx, yy, dire)
if res == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
def check(s, x, y, xx, yy, dire):
  if len(s) != 0:
    i = 0
    for command in s:
      i += 1
      if command == "F":
        xx += dire[0]
        yy += dire[1]
      else:
        if dire[0] != 0:
          if check(s[i:], x, y, xx, yy, [0, 1]) == 1:
          	return 1
          if check(s[i:], x, y, xx, yy, [0, -1]) == 1:
            return 1
        elif dire[1] != 0:
          if check(s[i:], x, y, xx, yy, [1, 0]) == 1:
            return 1
          if check(s[i:], x, y, xx, yy, [-1, 0]) == 1:
            return 1
        return 0
  if xx == x and yy == y:
    return 1
  else:
    return 0


s = input()
x, y = map(int, input().split())
xx, yy = 0, 0
dire = [1, 0] #右1左-1,上1下-1
res = check(s, x, y, xx, yy, dire)
if res == 1:
  print("Yes")
else:
  print("No")

Submission status: Runtime Error

Solution code:
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = input()
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    flag=1
    x1 = []
    y1 = []
    t= 0
    for i in range(len(s)):
        if i%2==0:
            if flag==1:
              if i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
            t+=s[i]
    x2 = sum(x1)+s[0]
    y2= sum(y1)
    m = max(x2,y2,t)
    dpx=[[-1]*(m*2+5) for i in range(len(x1)+1)]
    dpx[0][m+s[0]]=1
    dpy =[[-1]*(m*2+5) for i in range(len(y1)+1)]
    dpy[0][m]=1
    for i in range(len(x1)):
        for j in range(m*2):
          if dpx[i][j]!=-1:
            if j+x1[i]<=(m)*2:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(m*2):
          if dpy[i][j]!=-1:
            if j+y1[i]<=m*2:
                dpy[i+1][j+y1[i]]=1
                #print(j+y1[i])
            if j-y1[i]>=0:
                dpy[i+1][j-y1[i]]=1
                #print(j-y1[i])
    if dpx[len(x1)][m+x]==1 and dpy[len(y1)][m+y]==1:
        print("Yes")
    else:
        print("No")
main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = input()
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    flag=1
    x1 = []
    y1 = []
    for i in range(len(s)):
        if i%2==0:
            if flag==1:
              if i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
    x2 = sum(x1)+s[0]
    y2= sum(y1)
    m = max(x2,y2)
    dpx=[[-1]*(m*2+5) for i in range(len(x1)+1)]
    dpx[0][m+s[0]]=1
    dpy =[[-1]*(m*2+5) for i in range(len(y1)+1)]
    dpy[0][m]=1
    for i in range(len(x1)):
        for j in range(m*2):
          if dpx[i][j]!=-1:
            if j+x1[i]<=(m)*2:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(m*2):
          if dpy[i][j]!=-1:
            if j+y1[i]<=m*2:
                dpy[i+1][j+y1[i]]=1
                #print(j+y1[i])
            if j-y1[i]>=0:
                dpy[i+1][j-y1[i]]=1
                #print(j-y1[i])
    if dpx[len(x1)][m+x]==1 and dpy[len(y1)][m+y]==1:
        print("Yes")
    else:
        print("No")
main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = input()
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    flag=1
    x1 = []
    y1 = []
    for i in range(len(s)):
        if i%2==0:
            if flag==1:
              if i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
    x2 = sum(x1)
    y2= sum(y1)
    dpx=[[-1]*((x2+s[0])*2+5) for i in range(len(x1)+1)]
    dpx[0][x2+s[0]]=1
    dpy =[[-1]*(y2*2+5) for i in range(len(y1)+1)]
    dpy[0][sum(y1)]=1
    for i in range(len(x1)):
        for j in range((x2+s[0])*2):
            if j+x1[i]<=(x2+s[0])*2 and dpx[i][j]!=-1:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0 and dpx[i][j]!=-1:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(y2*2):
            if j+y1[i]<=y2*2 and dpy[i][j]!=-1:
                dpy[i+1][j+y1[i]]=1
                #print(j+y1[i])
            if j-y1[i]>=0 and dpy[i][j]!=-1:
                dpy[i+1][j-y1[i]]=1
                #print(j-y1[i])
    
    if dpx[len(x1)][x2+x]==1 and dpy[len(y1)][y2+y]==1:
        print("Yes")
    else:
        print("No")
main()

Submission status: Runtime Error

Solution code:
s = input().strip()
x,y = map(int,input().split())
A = [0]
B = [0]
cur = 0
cnt = 0
for i in range(len(s)):
    if s[i]!="T":
        cnt += 1
    else:
        if cur==0 and cnt>0:
            A.append(cnt)
        elif cur==1 and cnt>0:
            B.append(cnt)
        cnt = 0
        cur = 1-cur
if s[-1]!="T":
    if cur==0:
        A.append(cnt)
    elif cur==1:
        B.append(cnt)
A = sorted(A)
B = sorted(B)
dpA = [[-1 for _ in range(4001)] for _ in range(len(A)+1)]
dpB = [[-1 for _ in range(4001)] for _ in range(len(B)+1)]
dpA[0][0] = 0
for i in range(len(A)):
    for j in range(4001):
        if j==0:
            dpA[i+1][j] = 1
        else:
            if dpA[i][j]>=0:
                dpA[i+1][j] = 1
            elif j<A[i] or dpA[i+1][j-A[i]]<=0:
                dpA[i+1][j] = -1
            else:
                dpA[i+1][j] = dpA[i+1][j-A[i]]-1
for i in range(len(B)):
    for j in range(4001):
        if j==0:
            dpB[i+1][j] = 1
        else:
            if dpB[i][j]>=0:
                dpB[i+1][j] = 1
            elif j<B[i] or dpB[i+1][j-B[i]]<=0:
                dpB[i+1][j] = -1
            else:
                dpB[i+1][j] = dpB[i+1][j-B[i]]-1
if (sum(A)-x)%2==1 or (sum(B)-y)%2==1:
    print("No")
else:
    nA = (sum(A)-x)//2
    nB = (sum(B)-y)//2
    flagA = dpA[len(A)][nA]
    flagB = dpB[len(B)][nB]
    if flagA>=0 and flagB>=0:
        print("Yes")
    else:
        print("No")

Submission status: Runtime Error

Solution code:
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = list(input())
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    #print(s)
    flag=1
    x1 = []
    y1 = []
    ansx=s[0]
    ansy=0
    flagx=0
    flagy= 0
    for i in range(len(s)):
        if i%2==0:
            if flag==1:
              if i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
    dpx=[[-1]*(len(s)*2+5) for i in range(len(x1)+1)]
    dpx[0][len(s)+s[0]]=1
    dpy =[[0]*(len(s)*2+5) for i in range(len(y1)+1)]
    dpy[0][len(s)]=1
    for i in range(len(x1)):
        for j in range(len(s)*2+2):
            if j+x1[i]<=len(s)*2+2 and dpx[i][j]!=-1:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0 and dpx[i][j]!=-1:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(len(s)*2+5):
            if j+y1[i]<len(s)*2+5 and dpy[i][j]!=-1:
                dpy[i+1][j+y1[i]]=1
            elif j-y1[i]>=0 and dpy[i][j]!=-1:
                dpy[i+1][j-y1[i]]=1
    
    if dpx[len(x1)][len(s)+x]==1 and dpy[len(y1)][len(s)+y]==1:
        print("Yes")
    else:
        print("No")
main()

Submission status: Runtime Error

Solution code:
import sys
import itertools
from itertools import accumulate
def main():
    import itertools
    def eratosthenes_sieve(n):
        table = [0] * (n + 1)
        prime_list = []
        for i in range(2, n + 1):
            if table[i] == 0:
                prime_list.append(i)
                for j in range(i + i, n + 1, i):
                    table[j] = 1
        return prime_list
        def inputs():return (int(x) for x in input().split())
    input = sys.stdin.readline
    def inputs():
        return [int(x) for x in input().split()]
    S = list(input())
    x,y=inputs()
    cnt=0
    s =[]
    if S[0]=="T":
        s.append(0)
        cnt+=1
    else:
        cnt+=1
    for i in range(len(S)-2):
        if S[i]==S[i+1]:
            cnt+=1
        else:
            s.append(cnt)
            cnt=1
    s.append(cnt)
    #print(s)
    flag=1
    x1 = []
    y1 = []
    ansx=s[0]
    ansy=0
    flagx=0
    flagy= 0
    for i in range(len(s)):
        if i%2==0:
            if flag==1 and i!=0:
                x1.append(s[i])
            else:
                y1.append(s[i])
        else:
            flag=(flag+s[i])%2
    dpx=[[-1]*(len(s)*2+5) for i in range(len(x1)+1)]
    dpx[0][len(s)+s[0]]=1
    dpy =[[0]*(len(s)*2+5) for i in range(len(y1)+1)]
    dpy[0][len(s)]=1
    for i in range(len(x1)):
        for j in range(len(s)*2+2):
            if j+x1[i]<=len(s)*2+2 and dpx[i][j]!=-1:
                dpx[i+1][j+x1[i]]=1
            if j-x1[i]>=0 and dpx[i][j]!=-1:
                dpx[i+1][j-x1[i]]=1      
    for i in range(len(y1)):
        for j in range(len(s)*2+5):
            if j+y1[i]<len(s)*2+5 and dpy[i][j]!=-1:
                dpy[i+1][j+y1[i]]=1
            elif j-y1[i]>=0 and dpy[i][j]!=-1:
                dpy[i+1][j-y1[i]]=1
    
    if dpx[len(x1)][len(s)+x]==1 and dpy[len(y1)][len(s)+y]==1:
        print("Yes")
    else:
        print("No")
main()