Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from copy import copy
from itertools import product
h, w = map(int, input().split())
maze = []
max_d = 0
for i in range(h):
    maze.append(input())
for i, j in product(range(h), range(w)):
    if maze[i][j] == '#':
        continue
    s = (i, j)
    for k, l in product(range(h), range(w)):
        if maze[k][l] == '#':
            continue
        g = (k, l)
        pathes = [[s]]
        while pathes:
            path = pathes.pop(0)
            a, b = path[-1]
            for point in [(0, 1), (1, 0), (0, -1),(-1, 0)]:
                di, dj = point
                if a + di >= 0 and a + di < h and b + dj >= 0 and b + dj < w and (a+di, b+dj) not in path and maze[a+di][b+dj]=='.':
                    copy_path = copy(path)
                    copy_path.append((a + di, b + dj))
                    pathes.append(copy_path)
                if (a + di, b + dj) == g:
                    max_d = max(max_d, len(copy_path)-1)
                    pathes = None
                    break
print(max_d)
                



Fixed submission:
from copy import copy
from itertools import product
h, w = map(int, input().split())
maze = []
max_d = 0
for i in range(h):
    maze.append(input())
for i, j in product(range(h), range(w)):
    if maze[i][j] == '#':
        continue
    s = (i, j)
    pathes = [[s]]
    seen = {s}
    while pathes:
        path = pathes.pop(0)
        a, b = path[-1]
        for point in [(0, 1), (1, 0), (0, -1),(-1, 0)]:
            di, dj = point
            if a + di >= 0 and a + di < h and b + dj >= 0 and b + dj < w and (a+di, b+dj) not in seen and maze[a+di][b+dj]=='.':
                copy_path = copy(path)
                copy_path.append((a + di, b + dj))
                seen.add((a+di, b+dj))
                pathes.append(copy_path)
                max_d = max(max_d, len(copy_path)-1)
print(max_d)
Buggy python submission (verdict: Runtime Error):
from math import gcd
def lcm(a, b):
    return a * b / gcd(a,b)
a, b = map(int, input().split())
print(int(lcm(a, b)))

Fixed submission:
from fractions import gcd
def lcm(a, b):
    return a * b / gcd(a,b)
a, b = map(int, input().split())
print(int(lcm(a, b)))
Buggy python submission (verdict: Runtime Error):
n = int(input())
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append([b,i])
ans = [None] * (n-1)

def dfs(now, color):
    cnt = 1
    for to, num in g[now]:
        if cnt == color:
            cnt += 1
        ans[num] = cnt
        dfs(to, cnt)
        cnt += 1
dfs(0, 0)
print(max(ans))
print(*ans, sep='\n')

Fixed submission:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)

n = int(input())
g = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    g[a].append([b,i])
ans = [None] * (n-1)

def dfs(now, color):
    cnt = 1
    for to, num in g[now]:
        if cnt == color:
            cnt += 1
        ans[num] = cnt
        dfs(to, cnt)
        cnt += 1
dfs(0, 0)
print(max(ans))
print(*ans, sep='\n')
Buggy python submission (verdict: Runtime Error):
999999 999999x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
    exit()
a = (x + y) // 3
x -= a
y -= a
mod = 10 ** 9 + 7
def combination(n, r):
    x = 1
    y = 1
    for i in range(r):
        x = x * (n - i) % mod
        y = y * (r - i) % mod
    return (x * pow(y, mod-2, mod)) % mod
print(combination(x+y, min(x, y)))

Fixed submission:
x, y = map(int, input().split())
if (x + y) % 3 != 0:
    print(0)
    exit()
a = (x + y) // 3
x -= a
y -= a
if x < 0 or y < 0:
    print(0)
    exit()
mod = 10 ** 9 + 7
def combination(n, r):
    x = 1
    y = 1
    for i in range(r):
        x = x * (n - i) % mod
        y = y * (r - i) % mod
    return (x * pow(y, mod-2, mod)) % mod
print(combination(x+y, min(x, y)))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ns = list(map(int, input().split()))
ns = sorted(ns)
rslt = 0
for i in range(n):
    for j in range(i+1, n):
        a = ns[i]+ns[j]
        b = abs(ns[i]-ns[j])
        for k in range(j+1, n):
            if ns[k] < a and ns[k] > b:
                rslt += 1
            elif ns[k] >= a:
                break
print(rslt)

Fixed submission:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
rslt = 0
for i in range(n-1):
    a = l[i]
    for j in range(i+1, n):
        b = l[j]
        c = bisect.bisect_left(l, a+b)
        rslt += c-j-1
print(rslt)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ns = list(map(int, input().split()))
ns = sorted(ns)
rslt = 0
for i in range(n):
    for j in range(i+1, n):
        a = ns[i]+ns[j]
        b = abs(ns[i]-ns[j])
        for k in range(j+1, n):
            if ns[k] < a and ns[k] > b:
                rslt += 1
            elif ns[k] >= a:
                break
print(rslt)

Fixed submission:
import bisect
n = int(input())
l = list(map(int, input().split()))
l.sort()
rslt = 0
for i in range(n-1):
    a = l[i]
    for j in range(i+1, n):
        b = l[j]
        c = bisect.bisect_left(l, a+b)
        rslt += c-j-1
print(rslt)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import heapq
n, m = map(int, input().split())
d = defaultdict(list)
for i in range(n):
    a, b = map(int, input().split())
    d[a-1].append(-b)
cnt = 0
a = []
for i in range(m):
    a += d[i]
    heapq.heapify(a)
    if a:
        cnt += -heapq.heappop(a)
print(cnt)

Fixed submission:
from collections import defaultdict
import heapq
n, m = map(int, input().split())
d = defaultdict(list)
for i in range(n):
    a, b = map(int, input().split())
    d[a-1].append(-b)
cnt = 0
a = []
heapq.heapify(a)
for i in range(m):
    for j in d[i]:
        heapq.heappush(a, j)
    if a:
        cnt += -heapq.heappop(a)
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = map(int, input().split())
d = defaultdict(list)
for i in range(n):
    a, b = map(int, input().split())
    d[a].append(b)
for i in range(m):
    d[i+1] = sorted(d[i+1])
cnt = 0
for i in range(m, 0, -1):
    max_n = 0
    index = 0
    for j in range(i, 0, -1):
        if d[j] and max_n < d[j][-1]:
            index = j
            max_n = d[j][-1]
    if d[index]:
        cnt += d[index].pop()
print(cnt)



Fixed submission:
from collections import defaultdict
import heapq
n, m = map(int, input().split())
d = defaultdict(list)
for i in range(n):
    a, b = map(int, input().split())
    d[a-1].append(-b)
cnt = 0
a = []
heapq.heapify(a)
for i in range(m):
    for j in d[i]:
        heapq.heappush(a, j)
    if a:
        cnt += -heapq.heappop(a)
print(cnt)
Buggy python submission (verdict: Runtime Error):
s = input()
n = len(s)
rslt = [0] * n
dic = {0:0, 1:1}
for i in range(1, n):
    if s[i] == 'L':
        rslt[i] += dic[(i+1)%2]
        rslt[i-1] += dic[i%2]
        dic[1] = 0
        dic[0] = 0
    else:
        dic[(i+1)%2] += 1
for i in range(n-1, 0, -1):
    if s[i] == 'R':
        rslt[i] += dic[(i+1)%2]
        rslt[i+1] += dic[i%2]
        dic[1] = 0
        dic[0] = 0
    else:
        dic[(i+1)%2] += 1
print(' '.join(rslt))


Fixed submission:
from math import ceil
s = input()
n = len(s)
rslt = [0] * n
dic = {0:0, 1:0}
cnt = 0
for i in range(0, n):
    if s[i] == 'R':
        cnt += 1
    else:
        rslt[i] += cnt//2
        rslt[i-1] += ceil(cnt/2)
        cnt = 0
for i in range(n-1, -1, -1):
    if s[i] == 'L':
        cnt += 1
    else:
        rslt[i] += cnt//2
        rslt[i+1] += ceil(cnt/2)
        cnt = 0
print(' '.join(list(map(str, rslt))))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
for i in range(n):
    print(max(l[:i]+l[i+1:]))

Fixed submission:
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
l2 = sorted(l)
a1 = l2[-1]
a2 = l2[-2]
for i in l:
    if i == a1:
        print(a2)
    else:
        print(a1)
Buggy python submission (verdict: Runtime Error):
from itertools import combinations
n, d = map(int, input().split())
l = []
count = 0
for i in range(n):
    l.append(list(map(int, input().split())))
for a, b in combinations(l, 2):
    if ((a[0]-b[0])**2+(a[1]-b[1])**2)**(1/2) % 1 == 0:
        count += 1
print(count)        

Fixed submission:
from itertools import combinations
n, d = map(int, input().split())
l = []
count = 0
for i in range(n):
    l.append(list(map(int, input().split())))
for a, b in combinations(l, 2):
    if (sum((a[i]-b[i])**2 for i in range(d)))**(1/2) % 1 == 0:
        count += 1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ns = list(map(int, input().split()))

rslt = []
ttl = sum(ns)
for i in range(n):
    rslt.append(ttl - 2 * sum(ns[i % 2:max(i - 1, 0):2] + ns[i + 1::2]))
print(*rslt)

Fixed submission:
n = int(input())
ns = list(map(int, input().split()))

rslt = []
ttl = sum(ns)
x = ttl - 2 * sum(ns[i] for i in range(1, n, 2))
rslt.append(x)
for i in range(0, n-1):
    x = 2 * ns[i] - x
    rslt.append(x)
print(*rslt)
Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
ns = set(i+1 for i in range(n))
for i in range(m):
    l, r = map(int, input().split())
    ns &= set(j for j in range(l, r+1))
print(len(ns))

Fixed submission:
n,m = map(int, input().split())
ns = set(i+1 for i in range(n))
l_min = 0
r_max = n
for i in range(m):
    l, r = map(int, input().split())
    l_min = max(l_min, l)
    r_max = min(r_max, r)
print(max(r_max - l_min+1, 0))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input()
one = [i.count('1') for i in s.split('0') if i.count('1') > 0]
zero = [i.count('0') for i in s.split('1') if i.count('0') > 0]
if s[0] == '1':
    nums = sum([[i, j] for i, j in zip(one,zero)], [])
    if s[-1] == '1':
        nums.append(one[-1])
    a = sum(nums[:k*2+1])
    rslt = a
    for i in range(k*2+1,len(nums), 2):
        a += sum(nums[i:i+2])
        a -= sum(nums[i-2*k-1:i-2*k+1])        
        rslt = max(rslt, a)
else:
    nums = sum([[i, j] for i, j in zip(zero,one)], [])
    if s[-1] == '0':
        nums.append(zero[-1])
    a = sum(nums[:k*2])
    rslt = a
    for i in range(k*2, len(nums), 2):
        a += sum(nums[i:i+2])
        a -= sum(nums[max(0,i-2*k-1):i-2*k+1])
        rslt = max(rslt, a)
print(rslt)

Fixed submission:
n, k = map(int, input().split())
s = input()
l = []
c = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        l.append(c)
        c = 0
    c += 1
l.append(c)
l += [0]
if s[0] == '0':
    l = [0] + l
tmp = sum(l[:2 * k + 1])
rslt = tmp
for i in range(0, len(l) - 2*k-2, 2):
    tmp = tmp - l[i] - l[i + 1] + l[i + 2 * k + 1] + l[i + 2 * k + 2]
    rslt = max(tmp, rslt)
print(rslt)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input()
one = [i.count('1') for i in s.split('0') if i.count('1') > 0]
zero = [i.count('0') for i in s.split('1') if i.count('0') > 0]
if s[0] == '0':
    a = sum(one[:k]+zero[:k])
    rslt = a
    for i in range(k, len(zero)):
        if len(zero) > len(one) and i == len(zero)-1:
            a += zero[i]
            a -= zero[i-k]
            rslt = max(rslt, a)
        else:
            a += one[i] + zero[i]       
            a -= one[i-k] + zero[i-k]
            rslt = max(rslt, a)
else:
    a = sum(one[:k+1]+zero[:k])
    rslt = a
    for i in range(k, len(zero)):
        a += one[i+1]+zero[i]
        a -= one[i-k] + zero[i-k]
        rslt = max(rslt, a)
print(rslt)
    

Fixed submission:
n, k = map(int, input().split())
s = input()
l = []
c = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        l.append(c)
        c = 0
    c += 1
l.append(c)
l += [0]
if s[0] == '0':
    l = [0] + l
tmp = sum(l[:2 * k + 1])
rslt = tmp
for i in range(0, len(l) - 2*k-2, 2):
    tmp = tmp - l[i] - l[i + 1] + l[i + 2 * k + 1] + l[i + 2 * k + 2]
    rslt = max(tmp, rslt)
print(rslt)
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
sl = len(s)
ac_cnts = []
for i in range(1, sl+1):
    ac_cnts.append(len(s[:i].split('AC'))-1)
for i in range(q):
    l, r = map(int, input().split())
    print(ac_cnts[r-1]-ac_cnts[l-1])

Fixed submission:
n, q = map(int, input().split())
s = input()
sl = len(s)
ac_cnts = [0]
cnt = 0
for i in range(0, sl-1):
    if s[i] == 'A' and s[i+1] == 'C':
        cnt += 1
    ac_cnts.append(cnt)
for i in range(q):
    l, r = map(int, input().split())
    print(ac_cnts[r-1]-ac_cnts[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
for i in range(q):
    l, r = map(int, input().split())
    print(len(s[l-1:r].split('AC'))-1)

Fixed submission:
n, q = map(int, input().split())
s = input()
sl = len(s)
ac_cnts = [0]
cnt = 0
for i in range(0, sl-1):
    if s[i] == 'A' and s[i+1] == 'C':
        cnt += 1
    ac_cnts.append(cnt)
for i in range(q):
    l, r = map(int, input().split())
    print(ac_cnts[r-1]-ac_cnts[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
n, m = map(int, input().split())
l = []
dic = defaultdict(list)
for i in range(m):
    p, y = map(int, input().split())
    dic[p].append(y)
    l.append([p, y])
for key, value in dic.items():
    dic[key] = sorted(value)
for p, y in l:
    print(str(p).zfill(6)+str(dic[p].index(y)+1).zfill(6))


Fixed submission:
from collections import defaultdict
n, m = map(int, input().split())
l = []
dic = defaultdict(list)
for i in range(m):
    p, y = map(int, input().split())
    dic[p].append(y)
    l.append([p, y])
for key, value in dic.items():
    dic[key] = {num: index+1 for index,num in enumerate(sorted(value))}
for p, y in l:
    print(str(p).zfill(6)+str(dic[p][y]).zfill(6))
Buggy python submission (verdict: Runtime Error):
from itertools import combinations
def dis(xi, xj, yi, yj):
    return abs(xi - xj) + abs(yi - yj)
n = int(input())
coords = []
grids = [(i, j) for i in range(101) for j in range(101)]
grids_set = set(grids)
for i in range(n):
    coords.append(tuple(map(int, input().split())))
xr, yr, hr = coords[0]
for i, j in combinations(coords, 2):
    xi, yi, hi = i
    xj, yj, hj = j
    for grid in grids:
        x, y = grid
        if dis(xi, x, yi, y) + hi != dis(xj, x, yj, y) + hj:
            grids_set.remove(grid)
    grids = list(grids_set)
x, y = grids[0]
print(x, y, dis(x, xr, y, yr)+hr)


Fixed submission:
def dis(xi, xj, yi, yj):
    return abs(xi - xj) + abs(yi - yj)
n = int(input())
coords = [tuple(map(int, input().split())) for _ in range(n)]
grids = [(i, j) for i in range(101) for j in range(101)]
for x, y, h in coords:
    if h > 0:
        break    
for xj, yj in grids:
    flag = True
    base = dis(x, xj, y, yj) + h
    for xi, yi, hi in coords:
        if hi != max(base-abs(xi-xj)-abs(yi-yj), 0):
            flag = False
    if flag:
        break
print(xj, yj, dis(x, xj, y, yj) + h)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
cnt = n
num = m//n
while num > 1:
    if m % num == 0 and num*cnt <= m:
        break
    else:
        num -= 1
print(num)

Fixed submission:
n, m = map(int, input().split())
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
div = make_divisors(m)
for i in div[::-1]:
    if i <= m / n:
        print(i)
        break
Buggy python submission (verdict: Runtime Error):
from fraction import gcd
from functools import reduce
def gcd_list(xs):
    return reduce(gcd, xs)

n, x = map(int, input().split())
xs = [abs(x-i) for i in map(int, input().split())]
print(gcd_list(xs))

Fixed submission:
from fractions import gcd
from functools import reduce
def gcd_list(xs):
    return reduce(gcd, xs)

n, x = map(int, input().split())
xs = [abs(x-i) for i in map(int, input().split())]
print(gcd_list(xs))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
ns = list(map(int, input().split()))
min_num = float('inf')
for i in range(n - k+1):
    l = ns[i:i + k]
    if l[-1] < 0:
        min_num = min(min_num, abs(l[0]))
    elif l[0] < 0 and l[-1] >= 0:
        min_num = min(min_num, abs(l[0]) * 2 + l[-1])
        min_num = min(min_num, abs(l[0]) + l[-1] * 2)
    else:
        min_num = min(min_num, l[-1])
print(min_num)

Fixed submission:
n, k = map(int, input().split())
ns = list(map(int, input().split()))
min_num = float('inf')
for i in range(n - k+1):
    l = ns[i]
    r = ns[i+k-1]
    min_num = min(min_num, abs(l) + abs(r-l), abs(r) + abs(l-r))
print(min_num)
Buggy python submission (verdict: Runtime Error):
n = int(input())
s = ''
value = 0
count = 0
while value != n:
    r = n % 2**(count+1)
    if (value + (-2)**count)%(2**(count+1)) == r:
        s += '1'
        value += (-2)**count
    else:
        s += '0'
    count += 1
print(int(s[::-1]))
        

Fixed submission:
n = int(input())
s = ''
value = 0
count = 0
while value != n:
    r = n % 2**(count+1)
    if (value + (-2)**count)%(2**(count+1)) == r:
        s += '1'
        value += (-2)**count
    else:
        s += '0'
    count += 1
if n == 0:
    print(0)
else:
    print(int(s[::-1]))
Buggy python submission (verdict: Runtime Error):
n = int(input())
ns = map(int, input().split())
print(sum(ns)-len(ns))

Fixed submission:
n = int(input())
ns = list(map(int, input().split()))
print(sum(ns)-len(ns))
Buggy python submission (verdict: Runtime Error):
from itertools import product
n, m = map(int,input().split())
canvas = []
for i in range(n):
    canvas.append(input())
for i,j in product([num for num in range(n)],[num for num in range(m)]):
    if canvas[i][j] == '.':
        continue
    result = 0
    if i > 0 and canvas[i-1][j] == '#':
        result += 1
    if i < n-1 and canvas[i+1][j] == '#':
        result += 1
    if j > 0 and canvas[i][j-1] == '#':
        result += 1
    if j < m-1 and canvas[i][j+1] == '#':
        result += 1
    if result == 0:
        flag = True
        break
if flag:
    print('No')
else:
    print('Yes')
        
            

Fixed submission:
from itertools import product
n, m = map(int,input().split())
canvas = []
flag = False
for i in range(n):
    canvas.append(input())
for i,j in product([num for num in range(n)],[num for num in range(m)]):
    if canvas[i][j] == '.':
        continue
    result = 0
    if i > 0 and canvas[i-1][j] == '#':
        result += 1
    if i < n-1 and canvas[i+1][j] == '#':
        result += 1
    if j > 0 and canvas[i][j-1] == '#':
        result += 1
    if j < m-1 and canvas[i][j+1] == '#':
        result += 1
    if result == 0:
        flag = True
        break
if flag:
    print('No')
else:
    print('Yes')
Buggy python submission (verdict: Runtime Error):
10 0def main():
    n, k = map(int, input().split())
    if k == 0:
        print(n ** 2)
        exit()
    cnt = 0
    for i in range(1, n+1):
        p, r = divmod(n, i)
        cnt += max(0, p * (i - k)) + max(0, r - k + 1)
    
    print(cnt)
if __name__=='__main__':
    main()

Fixed submission:
def main():
    n, k = map(int, input().split())
    if k == 0:
        print(n ** 2)
        exit()
    cnt = 0
    for i in range(1, n+1):
        p, r = divmod(n, i)
        cnt += max(0, p * (i - k)) + max(0, r - k + 1)
    
    print(cnt)
if __name__=='__main__':
    main()
Buggy python submission (verdict: Time Limit Exceeded):
import math
def using_sqrt(limit):
    n = 0
    primes = set()
    for k in range(2, limit+1):
        factor = 0
        
        # 2以外の偶数は素数ではないので無視する
        if k % 2 == 0 and k != 2:
            continue
        
        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            primes.add(k)
            
    return primes

primes = using_sqrt(10**5)

q = int(input())
for i in range(q):
    l, r = map(int,input().split())
    count = 0
    for j in range(l, r+1, 2):
        count += (j in primes and (j+1)/2 in primes )
    print(count)

Fixed submission:
import math
def using_sqrt(limit):
    n = 0
    primes = set()
    for k in range(2, limit+1):
        factor = 0
        
        # 2以外の偶数は素数ではないので無視する
        if k % 2 == 0 and k != 2:
            continue
        
        # 繰り返しの上限を対象の平方根にする
        for divisor in range(2, math.floor(math.sqrt(k))+1):
            if k % divisor == 0:
                factor += 1
                
        if factor == 0:
            primes.add(k)
            
    return primes

primes = using_sqrt(10**5+1)

likely = []
count = 0
for i in range(10**5+1):
    if i in primes and (i+1)/2 in primes:
        count += 1
    likely.append(count)
        
q = int(input())
    
for i in range(q):
    l, r = map(int,input().split())
    print(likely[r]-likely[l-1])
Buggy python submission (verdict: Runtime Error):
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')



Fixed submission:
s = input() + 'T'
n = len(s)
x, y = map(int, input().split())
X = [[], [0]]
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        X[xy].append(cnt)
        xy = 1 - xy
        cnt = 0

def f(l):
    if len(l) == 0:
        return [0]
    post = [l[0]]
    for i in range(1, len(l)):
        if l[i]:
            pre = sorted(post)
            post = []
            for j in range(len(pre)):
                if j == 0 or pre[j] != pre[j-1]:
                    post.append(pre[j]+l[i])
                    post.append(pre[j]-l[i])
    return post

print('Yes' if x in f(X[0]) and y in f(X[1]) else 'No')

Buggy python submission (verdict: Runtime Error):
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')



Fixed submission:
s = input() + 'T'
n = len(s)
x, y = map(int, input().split())
X = [[], [0]]
xy = 0
cnt =0
for i in s:
    if i == 'F':
        cnt += 1
    else:
        X[xy].append(cnt)
        xy = 1 - xy
        cnt = 0

def f(l):
    if len(l) == 0:
        return [0]
    post = [l[0]]
    for i in range(1, len(l)):
        if l[i]:
            pre = sorted(post)
            post = []
            for j in range(len(pre)):
                if j == 0 or pre[j] != pre[j-1]:
                    post.append(pre[j]+l[i])
                    post.append(pre[j]-l[i])
    return post

print('Yes' if x in f(X[0]) and y in f(X[1]) else 'No')