Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p03164 (python)

Fixed submission:
from sys import stdin, stdout
from collections import deque, defaultdict
import math as m

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n, W = rli()
	items = []
	maxval = 0
	for _ in range(n):
		w, v = rli()
		maxval += v
		items.append((w, v))

	dp = [INF for _ in range(maxval + 1)]
	dp[0] = 0
	for w, v in items:
		for i in range(maxval, -1, -1):
			if i - v >= 0:
				dp[i] = min(dp[i], dp[i-v] + w)
	ans = NINF
	for val, minweight in enumerate(dp):
		if minweight <= W:
			ans = val
	print(ans)
	stdout.close()

if __name__ == "__main__":
	main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout
rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n, W = rli()
	weight = [0 for _ in range(n)]
	val = [0 for _ in range(n)]
	for i in range(n):
		w, v = rli()
		weight[i], val[i] = w, v
	#dp(i, j) = max val using items 1,...,i at 
	#			weight j

	dp = [[NINF for c in range(W+1)] for r in range(n+1)]
	for r in range(n+1):
		dp[r][0] = 0
	for c in range(W+1):
		dp[0][c] = 0
	for r in range(1, n+1):
		w, v = weight[r-1], val[r-1]
		for c in range(1, W+1):
			if c - w < 0:
				dp[r][c] = max(dp[r][c], dp[r-1][c])
			else:
				dp[r][c] = max(dp[r][c], dp[r-1][c], v + dp[r-1][c-w])
	print(dp[-1][-1])
	stdout.close()

if __name__ == "__main__":
	main()
Problem p03165 (python)

Fixed submission:
from sys import stdin, stdout
from collections import deque, defaultdict
import math as m

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	s = rll()[0]
	t = rll()[0]
	m, n = len(s), len(t)
	dp = [[0 for __ in range(n)] for _ in range(m)]

	#dp[i][j] = LCs of s0,...,si and t0,...,tj

	def pprint(M):
		for row in M:
			print(row)
		print("~~~")
	
	dp[0][0] = 1 if s[0] == t[0] else 0
	for c in range(1, n):
		dp[0][c] = max(dp[0][c-1], 1 if s[0] == t[c] else 0)
	for r in range(1, m):
		dp[r][0] = max(dp[r-1][0], 1 if s[r] == t[0] else 0)

	for r in range(1, m):
		for c in range(1, n):
			dp[r][c] = max(dp[r][c], dp[r-1][c], dp[r][c-1])
			if s[r] == t[c]:
				dp[r][c] = max(dp[r][c], 1 + dp[r-1][c-1])

	ans = deque()
	i, j = m - 1, n - 1
	while i >= 0 and j >= 0:
		if s[i] == t[j]:
			ans.appendleft(s[i])
			i -= 1
			j -= 1
		else:
			up, left = NINF, NINF
			if i - 1 >= 0:
				up = dp[i-1][j]
			if j - 1 >= 0:
				left = dp[i][j-1]
			if up >= left:
				i -= 1
			else:
				j -= 1
	print("".join(x for x in ans))
	stdout.close()

if __name__ == "__main__":
	main()

Buggy submission (verdict: Runtime Error):
from sys import stdin, stdout
from collections import deque, defaultdict
import math as m

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	s = rll()[0]
	t = rll()[0]
	m, n = len(s), len(t)
	dp = [[0 for __ in range(n)] for _ in range(m)]

	#dp[i][j] = LCs of s0,...,si and t0,...,tj

	def pprint(M):
		for row in M:
			print(row)
		print("~~~")
	
	dp[0][0] = 1 if s[0] == t[0] else 0
	for c in range(1, n):
		dp[0][c] = max(dp[0][c-1], 1 if s[0] == t[c] else 0)
	for r in range(1, m):
		dp[r][0] = max(dp[r-1][0], 1 if s[r] == t[0] else 0)

	for r in range(1, m):
		for c in range(1, n):
			dp[r][c] = max(dp[r][c], dp[r-1][c], dp[r][c-1])
			if s[r] == t[c]:
				dp[r][c] = max(dp[r][c], 1 + dp[r-1][c-1])

	ans = deque()
	def reconstruct(i, j):
		if i < 0 or j < 0: return
		if s[i] == t[j]:
			ans.appendleft(s[i])
			reconstruct(i-1, j-1)
		else:
			up, left = NINF, NINF
			if i - 1 >= 0:
				up = dp[i-1][j]
			if j - 1 >= 0:
				left = dp[i][j-1]
			if up >= left:
				reconstruct(i-1, j)
			else:
				reconstruct(i, j-1)
	reconstruct(m-1, n-1)
	print("".join(x for x in ans))
	stdout.close()

if __name__ == "__main__":
	main()
Problem p02534 (python)

Fixed submission:
print("ACL" * int(input()))

Buggy submission (verdict: Runtime Error):
k = int(input())
print("ACL" * k
Problem p02612 (python)

Fixed submission:
n = int(input())
bez = 1000
while bez < n:
    bez += 1000
print(bez-n)

Buggy submission (verdict: Runtime Error):
print(int(input()%1000)
Problem p02534 (python)

Fixed submission:
num = int(input())
print(num * "ACL")

Buggy submission (verdict: Runtime Error):
num= int(input())
print(ACL*num)
Problem p02630 (python)

Fixed submission:
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    Q, *BC = map(int, read().split())

    total = sum(A)
    d = Counter(A)
    for b, c in zip(BC[::2], BC[1::2]):
        total -= b * d[b]
        total += c * d[b]
        d[c] += d[b]
        d[b] = 0
        print(total)

    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import Counter

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    Q, *BC = map(int, read().split())

    total = sum(A)
    d = [0] * (N+1)
    for a in A:
        d[a] += 1

    for b, c in zip(BC[::2], BC[1::2]):
        total += (c - b) * d[b]
        d[c] += d[b]
        d[b] = 0
        print(total)

    return


if __name__ == '__main__':
    main()
Problem p02632 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    K = int(readline())
    S = readline().strip()

    COM_MAX = 2 * 10 ** 6

    fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1

    for i in range(2, COM_MAX + 1):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def mod_com(n, r):
        if n < r:
            return 0
        if n < 0 or r < 0:
            return 0
        return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

    M = len(S)
    L = K + M

    ans = 0
    p = pow(25, K + 1, MOD)
    for i in range(M - 1, -1, -1):
        ans += mod_com(L, i) * p
        p = p * 25 % MOD

    ans = (pow(26, L, MOD) - ans) % MOD
    print(ans)
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    K = int(readline())
    S = readline().strip()

    COM_MAX = 10 ** 6

    fac, finv, inv = [0] * (COM_MAX + 1), [0] * (COM_MAX + 1), [0] * (COM_MAX + 1)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1

    for i in range(2, COM_MAX + 1):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD % i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    def mod_com(n, r):
        if n < r:
            return 0
        if n < 0 or r < 0:
            return 0
        return fac[n] * (finv[r] * finv[n - r] % MOD) % MOD

    M = len(S)
    L = K + M

    ans = 0
    p = pow(25, K + 1, MOD)
    for i in range(M - 1, -1, -1):
        ans += mod_com(L, i) * p
        p = p * 25 % MOD

    ans = (pow(26, L, MOD) - ans) % MOD
    print(ans)
    return


if __name__ == '__main__':
    main()
Problem p02641 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    X, N, *P = map(int, read().split())
    P = set(P)
    Q = [i for i in range(-10, 110) if i not in P]
    Q.sort()
    error = INF
    for q in Q:
        if abs(X-q) < error:
            ans = q
            error = abs(X-q)
    
    print(ans)
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    X, N, *P = map(int, read().split())
    P = set(P)
    Q = [i for i in range(1, 101) if i not in P]
    Q.sort()
    error = INF
    for q in Q:
        if abs(X-q) < error:
            ans = q
            error = abs(X-q)
    
    print(ans)
    return


if __name__ == '__main__':
    main()
Problem p02678 (python)

Fixed submission:
from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


N, M = map(int, input().split())
G = [[] for _ in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dist = [-1] * N
dist[0] = 0
prev = [0] * N
q = deque([0])
while q:
    v = q.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            q.append(nv)
            dist[nv] = dist[v] + 1
            prev[nv] = v
    
print('Yes')
for v in prev[1:]:
    print(v+1)

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


N, M = map(int, input().split())
A = [0] * M
B = [0] * M
G = [[] for _ in range(M)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

dist = [-1] * N
dist[0] = 0
prev = [0] * N
q = deque([0])
while q:
    v = q.popleft()
    for nv in G[v]:
        if dist[nv] == -1:
            q.append(nv)
            dist[nv] = dist[v] + 1
            prev[nv] = v
    
print('Yes')
for v in prev[1:]:
    print(v+1)

Buggy submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit
from heapq import heappush, heappop

setrecursionlimit(10 ** 9)
INF = 1 << 60


def input():
    return stdin.readline().strip()


N, M = map(int, input().split())
A = [0] * M
B = [0] * M
G = [[] for _ in range(M)]
for i in range(M):
    a, b = map(int, input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
    
dist = [INF] * N
prev = [0] * N

pq = [(0, 0)]
while pq:
    d, v = heappop(pq)
    if dist[v] < d:
        continue
    for nv in G[v]:
        if dist[nv] > d + 1:
            dist[nv] = d + 1
            heappush(pq, (dist[nv], nv))
            prev[nv] = v

print('Yes')
for v in prev[1:]:
    print(v+1)
Problem p02845 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = map(int, read().split())
    seq = [-1] * 3
    ans = 1
    try:
        for a in A:
            ans = ans * seq.count(a - 1) % MOD
            seq[seq.index(a - 1)] = a
        print(ans)
    except:
        print(0)
        
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = map(int, read().split())
    C = [0] * N
    C[0] = 3
    ans = 1
    for a in A:
        ans = ans * C[a] % MOD
        if ans == 0:
            print(0)
            return
        C[a] -= 1
        C[a + 1] += 1

    print(ans)

    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = map(int, read().split())
    seq = [-1] * 3
    ans = 1
    for a in A:
        ans = ans * seq.count(a - 1) % MOD
        seq[seq.index(a - 1)] = a

    print(ans)
    return


if __name__ == '__main__':
    main()
Problem p02861 (python)

Fixed submission:
from itertools import permutations
from math import factorial

N = int(input())
cities = [tuple(map(int, input().split())) for _ in range(N)]

dists = [[0] * N for _ in range(N)]
for i in range(N):
    for j in range(i + 1, N):
        dists[i][j] = dists[j][i] = (
            (cities[j][0] - cities[i][0]) ** 2 + (cities[j][1] - cities[i][1]) ** 2
        ) ** 0.5

total_dist = 0
for p in permutations(range(N)):
    for i in range(N - 1):
        total_dist += dists[p[i]][p[i + 1]]

print(total_dist / factorial(N))

Buggy submission (verdict: Runtime Error):
import sys
from itertools import permutations
from math import factorial

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def distance2(x1, y1, x2, y2):
    return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5


def main():
    N = int(readline())
    C = [tuple(map(int, readline().split())) for _ in range(N)]

    dist = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(i + 1, N):
            dist[i][j] = dist[j][i] = distance2(*C[i], *C[j])

    ans = 0
    for perm in permutations(range(N)):
        for i in range(N - 1):
            ans += dist[perm[i]][perm[i + 1]]

    print(ans / factorial(N))
    return


if __name__ == '__main__':
    main()
Problem p02923 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *H = map(int, read().split())

    ans = []
    c = 0
    for i in range(N - 1):
        if H[i] >= H[i + 1]:
            c += 1
        else:
            ans.append(c)
            c = 0

    ans.append(c)

    print(max(ans))
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *H = map(int, read().split())
    
    ans = []
    c = 0
    for i in range(N-1):
        if H[i] >= H[i+1]:
            c += 1
        else:
            ans.append(c)
            c = 0
            
    if c > 0:
        ans.append(c)
            
    print(max(ans))
    return


if __name__ == '__main__':
    main()
Problem p02982 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def distance2_nd(p, q):
    return sum((px - qx) ** 2 for px, qx in zip(p, q))


def isqrt(n):
    if n > 0:
        x = 1 << (n.bit_length() + 1) // 2
        y = (x + n // x) // 2
        while y < x:
            x = y
            y = (x + n // x) // 2
        return x
    elif n == 0:
        return 0
    else:
        raise ValueError("isqrt() argument must be nonnegative")


def main():
    N, D = map(int, readline().split())
    X = [list(map(int, readline().split())) for _ in range(N)]

    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            d2 = distance2_nd(X[i], X[j])
            di = isqrt(d2)
            if di * di == d2:
                ans += 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    L, R = map(int, readline().split())

    if L - R + 1 >= 2019:
        print(0)
    else:
        A = [i % 2019 for i in range(L, R + 1)]
        A.sort()
        print(A[0] * A[1])

    return


if __name__ == '__main__':
    main()
Problem p03043 (python)

Fixed submission:
import sys
from math import log2, ceil

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, K = map(int, readline().split())

    ans = 0
    for n in range(1, N + 1):
        if n >= K:
            ans += 1
        else:
            ans += pow(2, -ceil(log2(K / n)))

    print(ans / N)
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from math import log2, ceil

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N, K = map(int, readline().split())

    ans = 0
    for n in range(1, N + 1):
        if n >= K:
            ans += 1
        else:
            ans += pow(2, -ceil(log2(K / n)))

    print(ans / N)
    return


if __name__ == '__main__':
    main()
Problem p03230 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N = int(readline())
    K = (1 + (8 * N + 1) ** 0.5) / 2
    if not K.is_integer():
        print('No')
        return
    K = int(K)

    ans = [[] for _ in range(K)]
    num = 1
    for k in range(K):
        for l in range(k + 1, K):
            ans[k].append(num)
            ans[l].append(num)
            num += 1
    print('Yes')
    print(K)
    for row in ans:
        print(K - 1, *row)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N = int(readline())
    K = int((1 + (8 * N + 1) ** 0.5) / 2)
    if not N == K * (K - 1) // 2:
        print('No')
        return

    num = 1
    ans = [[0] * (K - 1) for _ in range(K - 1)]
    for i in range(K - 1):
        for j in range(i + 1):
            ans[i][j] = ans[j][i] = num
            num += 1
                
    ans.append([ans[i][i] for i in range(K - 1)])
    ans = [f"{K-1} {' '.join(map(str, row))}" for row in ans]
    
    print('Yes')
    print(K)
    print('\n'.join(ans))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def main():
    N = int(readline())
    if N == 3:
        print('Yes')
        print(3)
        print(2, 1, 2)
        print(2, 3, 1)4
        print(2, 2, 3)
    else:
        print('No')
    return


if __name__ == '__main__':
    main()
Problem p03450 (python)

Fixed submission:
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, *LRD = map(int, read().split())

    G = [[] for _ in range(N)]
    for l, r, d in zip(*[iter(LRD)] * 3):
        G[l - 1].append((r - 1, d))
        G[r - 1].append((l - 1, -d))

    dist = [INF] * N
    for i in range(N):
        if dist[i] != INF:
            continue

        dist[i] = 0
        queue = deque([i])
        while queue:
            v = queue.popleft()
            for nv, d in G[v]:
                if dist[nv] != INF:
                    if dist[nv] != dist[v] + d:
                        print('No')
                        return
                else:
                    dist[nv] = dist[v] + d
                    queue.append(nv)

    print('Yes')
    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


class UnionFindWeighted:
    # Reference: https://note.nkmk.me/python-union-find/
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.weight = [0] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            root = self.find(self.parents[x])
            self.weight[x] += self.weight[self.parents[x]]
            self.parents[x] = root
            return root

    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)

        if self.parents[rx] > self.parents[ry]:
            x, y, rx, ry, w = y, x, ry, rx, -w

        self.parents[rx] += self.parents[ry]
        self.parents[ry] = rx
        self.weight[ry] = self.weight[x] - self.weight[y] - w

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def diff(self, x, y):
        return self.weight[x] - self.weight[y]


def main():
    N, M, *LRD = map(int, read().split())

    uf = UnionFindWeighted(N)
    for l, r, d in zip(*[iter(LRD)] * 3):
        l -= 1
        r -= 1
        if uf.same(l, r) and uf.diff(l, r) != d:
            print('No')
            return
        else:
            uf.union(l, r, d)

    print('Yes')
    return


if __name__ == '__main__':
    main()
Problem p03488 (python)

Fixed submission:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    base = 8000
    dp = [False] * 16001
    dp[init_pos + base] = True

    for i in range(len(vec)):
        dp, dp_prev = [False] * 16001, dp
        for j in range(16001):
            if (
                j - vec[i] >= 0
                and dp_prev[j - vec[i]]
                or j + vec[i] <= 16000
                and dp_prev[j + vec[i]]
            ):
                dp[j] = True

    return dp[target_pos + base]


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    idx = len(S)
    for i, c in enumerate(S):
        if c == 'T':
            idx = i
            break

    init_x = idx
    step = [[0] for _ in range(2)]
    i = 0
    for c in S[idx:]:
        if c == 'T':
            i = 1 - i
            step[i].append(0)
        else:
            step[i][-1] += 1

    ok = check(step[0], init_x, x) and check(step[1], 0, y)

    if ok:
        print('Yes')
    else:
        print('No')

    return


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def check(vec, init_pos, target_pos):
    base = 8000
    N = len(vec)
    M = 2 * base + 1
    dp = [False] * M
    dp[init_pos + base] = True

    for i in range(N):
        dp, dp_prev = [False] * M, dp
        for j in range(M):
            if j - vec[i] >= 0 and dp_prev[j - vec[i]] or j + vec[i] <= 16000 and dp_prev[j + vec[i]]:
                dp[j] = True

    return dp[target_pos + base]


def main():
    S = readline().strip()
    x, y = map(int, readline().split())

    idx = len(S)
    for i, c in enumerate(S):
        if c == 'T':
            idx = i
            break

    init_x = idx
    step = [[] for _ in range(2)]
    i = 0
    for c in S[idx:]:
        if c == 'T':
            i = 1 - i
            step[i].append(0)
        else:
            step[i][-1] += 1

    if not step[0]:
        step[0].appned(0)
    if not step[1]:
        step[1].append(0)

    ok = check(step[0], init_x, x) and check(step[1], 0, y)

    if ok:
        print('Yes')
    else:
        print('No')

    return


if __name__ == '__main__':
    main()