Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import sys
from math import log10, floor
k = int(sys.stdin.readline().strip())
d = int(input())
digit_num = floor(log10(k)) + 1
MOD = 10**9+7
# memo_ls[i][j][k] : 上からi桁目まででMODがjでflgがkの個数
memo_ls = [[[0 for k in range(2)] for j in range(d)] for i in range(digit_num + 1)]
memo_ls[0][0][0] = 1
dic = {j : j % d for j in range(d)}
for i, n in enumerate(str(k), 1):
    n = int(n)
    for j in range(10):
        x = dic[j]
        for k in range(d):
            _from = k - x if k - x >= 0 else d + k - x
            if j == n:
                memo_ls[i][k][0] += memo_ls[i-1][_from][0]
                memo_ls[i][k][1] += memo_ls[i-1][_from][1]
            elif j < n:
                memo_ls[i][k][1] += memo_ls[i-1][_from][1] + memo_ls[i-1][_from][0]
            else:
                memo_ls[i][k][1] += memo_ls[i - 1][_from][1]
            memo_ls[i][k][0] %= MOD
            memo_ls[i][k][1] %= MOD
print(sum(memo_ls[-1][0]) - 1)

Fixed submission:
import sys
from math import log10, floor
K = input()
d = int(input())
digit_num = len(K)
MOD = 10**9+7
# memo_ls[i][j][k] : 上からi桁目まででMODがjでflgがkの個数
memo_ls = [[[0 for _k in range(2)] for j in range(d)] for i in range(digit_num + 1)]
memo_ls[0][0][0] = 1
dic = {j : j % d for j in range(10)}
for i, n in enumerate(K, 1):
    n = int(n)
    for j in range(10):
        for k in range(d):
            _from = (k - j) % d
            memo_ls[i][k][1] = (memo_ls[i][k][1] + memo_ls[i-1][_from][1]) % MOD
            if j == n:
                memo_ls[i][k][0] = (memo_ls[i-1][_from][0] + memo_ls[i][k][0]) % MOD
            elif j < n:
                memo_ls[i][k][1] = (memo_ls[i-1][_from][0] + memo_ls[i][k][1]) % MOD
print((sum(memo_ls[digit_num][0]) - 1) % MOD)
Buggy python submission (verdict: Runtime Error):
import sys
n, x = [int(i) for i in sys.stdin.readline().split()]
cur = 1
ls = [cur]
for i in range(50):
    cur = cur * 2 + 3
    ls.append(cur)

def f(n, x):
    if x == 1:
        return 0
    elif x <= ls[n-1]:
        return f(n-1, x-1)
    elif x == ls[n-1] + 1:
        return 1 + ls[n-1] // 2 + 1
    elif x < ls[n]:
        return 1 + ls[n-1] // 2 + 1 + f(n-1, x - ls[n-1] - 2)
    else:
        return ls[n] // 2 + 1

res = f(n, x)
print(res)

Fixed submission:
import sys
n, x = [int(i) for i in sys.stdin.readline().split()]
cur = 1
p = 1
ls = [cur]
p_ls = [p]
for i in range(50):
    cur = cur * 2 + 3
    p = p * 2 + 1
    ls.append(cur)
    p_ls.append(p)

def f(n, x):
    if n == 0:
        return 0 if x <= 0 else 1
    if x == 1:
        return 0
    elif x <= 1 + ls[n-1]:
        return f(n-1, x-1)
    elif x == ls[n-1] + 2:
        return 1 + p_ls[n-1]
    elif x < ls[n]:
        return 1 + p_ls[n-1] + f(n-1, x - ls[n-1] - 2)
    else:
        return p_ls[n]
res = f(n, x)
print(res)
Buggy python submission (verdict: Runtime Error):
N, M = [int(i) for i in input().split()]
data = list()
for i in range(M):
    p, y = [int(i) for i in input().split()]
    data.append([i, p, y])
data.sort(key=lambda x:(x[1],x[2]))
ind = 1
for j, (i, p, y) in enumerate(data):
    if data[j-1][1] != p:
        ind = 1
    data[j].append(ind)
    ind += 1
data.sort(key=lambda x:x[0])
for i, p, y, j in data:
    print(str(p).zfill(6) + str(j).zfill(6))

Fixed submission:
X, Y = [int(i) for i in input().split()]
print(X + Y // 2)
Buggy python submission (verdict: Runtime Error):
N = int(input())
len_N = len(N)
num = int(str(N)[0]) + 1
if int(str(num) * len_N) < N:
  print(str(num) * len_N)
else:
  print(str(num + 1) * (len(str(N))))

Fixed submission:
N = int(input())
len_N = len(str(N))
num = int(str(N)[0])
if int(str(num) * len_N) < N:
  print(str(num + 1) * len_N)
else:
  print(str(num) * (len(str(N))))
Buggy python submission (verdict: Time Limit Exceeded):
N, X = [int(i) for i in input().split()]
x_ls = [int(i) for i in input().split()]
x_ls = [abs(x - X) for x in x_ls]
while True:
    if all([x % min(x_ls) == 0 for x in x_ls]):
        break
    else:
        x_ls = [x % min(x_ls) for x in x_ls]
    x_ls = [x for x in x_ls if x != 0]
print(min(x_ls))

Fixed submission:
N, X = [int(i) for i in input().split()]
x_ls = [int(i) for i in input().split()]
x_ls = [abs(x - X) for x in x_ls]
while True:
    min_x = min(x_ls)
    x_ls = [x % min_x for x in x_ls]
    if not any(x_ls):
        break
    x_ls = [x for x in x_ls if x != 0]
print(min_x)

Buggy python submission (verdict: Runtime Error):
N, X = [int(i) for i in input().split()]
x_ls = [int(i) for i in input().split()]
x_ls = [abs(x - X) for x in x_ls]
while True:
    if all([x % min(x_ls) == 0 for x in x_ls]):
        break
    else:
        x_ls = [x % min(x_ls) for x in x_ls]
print(min(x_ls))

Fixed submission:
N, X = [int(i) for i in input().split()]
x_ls = [int(i) for i in input().split()]
x_ls = [abs(x - X) for x in x_ls]
while True:
    min_x = min(x_ls)
    x_ls = [x % min_x for x in x_ls]
    if not any(x_ls):
        break
    x_ls = [x for x in x_ls if x != 0]
print(min_x)
Buggy python submission (verdict: Runtime Error):
s = [int(i) for i in input().strip()]
k = int(input())
ind = 0
while k > 0:
    if s[ind] == 1:
        k -= 1
        ind += 1
    else:
        k = 0
print(s[ind])

Fixed submission:
s = [int(i) for i in input().strip()]
k = int(input())
ind = -1
while k > 0:
    ind += 1
    if s[ind] == 1:
        k -= 1
    else:
        k = 0
print(s[ind])
Buggy python submission (verdict: Runtime Error):
import math
n = int(input())
if n == 0:
    print(0)
else:
    base = math.floor(math.log2(abs(n)))
    res = ""
    for i in range(base+1, -1, -1):
        if i % 2 == 0:
            if -(-2)**(i-1) <= n <= -(-2) ** (i+1):
                res += "1"
                n -= (-2) ** i
            else:
                res += "0"
        else:
            if -(-2)**(i-1) >= n >= -(-2) ** (i+1):
                res += "1"
                n -= (-2) ** i
            else:
                res += "0"
    if res[0] == "0":
        print(res[1:])
    else:
        print(res)

Fixed submission:
n = int(input())
res = ""
cnt = 0
div = 0
if n == 0:
    print(0)
else:
    while n != 0:
        div = n % 2**(cnt+1)
        div = 1 if div > 0 else 0
        res = str(div) + res
        if div > 0:
            n -= (-2) ** cnt
        cnt += 1
    print(res)
Buggy python submission (verdict: Runtime Error):
import math
D, G = [int(i) for i in input().split()]
data_ls = list()
for i in range(D):
    data_ls.append([int(i) for i in input().split()])
ps, cs = zip(*data_ls)

def search(cnt, sum, flg, rest_max):
    if sum > G:
        return cnt
    if flg == len(data_ls):
        if sum + (ps[rest_max] - 1) * 100 * (rest_max + 1) >= G:
            return cnt + math.floor((G - sum) / (100 * (rest_max+1)))
        else:
            return 1e9
    cnt = min(search(cnt + ps[flg], sum + (ps[flg] * 100 * (flg+1) + cs[flg]), flg + 1, rest_max),
              search(cnt, sum, flg + 1, flg))
    return cnt

print(search(0,0,0,-1))

Fixed submission:
import math
D, G = [int(i) for i in input().split()]
data_ls = list()
for i in range(D):
    data_ls.append([int(i) for i in input().split()])
ps, cs = zip(*data_ls)

def search(cnt, sum, flg, rest_max):
    if sum >= G:
        return cnt
    if flg == len(data_ls):
        if sum + (ps[rest_max] - 1) * 100 * (rest_max + 1) >= G:
            return cnt + math.ceil((G - sum) / (100 * (rest_max+1)))
        else:
            return 1e9
    cnt = min(search(cnt + ps[flg], sum + (ps[flg] * 100 * (flg+1) + cs[flg]), flg + 1, rest_max),
              search(cnt, sum, flg + 1, flg))
    return cnt

print(search(0,0,0,0))
Buggy python submission (verdict: Time Limit Exceeded):
import heapq
import sys
n, m, s, t = [int(i) for i in input().split()]
s -= 1
t -= 1
snuuk_dict = dict()
yen_dict = dict()
snuuk_que = []
yen_que = []
for i in range(m):
    u, v, a, b = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if snuuk_dict.get(u) is None:
        snuuk_dict[u] = []
        yen_dict[u] = []
    if snuuk_dict.get(v) is None:
        snuuk_dict[v] = []
        yen_dict[v] = []
    snuuk_dict[u].append([b, u, v])
    snuuk_dict[v].append([b, v, u])
    yen_dict[u].append([a, u, v])
    yen_dict[v].append([a, v, u])

for i in snuuk_dict[t]:
    heapq.heappush(snuuk_que, i)
for i in yen_dict[s]:
    heapq.heappush(yen_que, i)
snuuk_ls = [-1 for i in range(n)]
yen_ls = [-1 for i in range(n)]
yen_ls[s] = 0
snuuk_ls[t] = 0
while len(yen_que) > 0:
    a, u, v = heapq.heappop(yen_que)
    if yen_ls[v] != -1:
        continue
    yen_ls[v] = a
    for i in yen_dict[v]:
        heapq.heappush(yen_que, [i[0] + yen_ls[v], i[1], i[2]])
while len(snuuk_que) > 0:
    b, v, u = heapq.heappop(snuuk_que)
    if snuuk_ls[u] != -1:
        continue
    snuuk_ls[u] = b
    for i in snuuk_dict[u]:
        heapq.heappush(snuuk_que,  [i[0] + snuuk_ls[u], i[1], i[2]])
sum_ls = [10** 15 - (i + j) for i, j in zip(snuuk_ls, yen_ls)]
for i in range(n):
    print(max(sum_ls[i:]))

Fixed submission:
import heapq
import sys
n, m, s, t = [int(i) for i in input().split()]
s -= 1
t -= 1
snuuk_dict = dict()
yen_dict = dict()
snuuk_que = []
yen_que = []
for i in range(m):
    u, v, a, b = [int(i) for i in sys.stdin.readline().split()]
    u -= 1
    v -= 1
    if snuuk_dict.get(u) is None:
        snuuk_dict[u] = []
        yen_dict[u] = []
    if snuuk_dict.get(v) is None:
        snuuk_dict[v] = []
        yen_dict[v] = []
    snuuk_dict[u].append([b, u, v])
    snuuk_dict[v].append([b, v, u])
    yen_dict[u].append([a, u, v])
    yen_dict[v].append([a, v, u])

for i in snuuk_dict[t]:
    heapq.heappush(snuuk_que, i)
for i in yen_dict[s]:
    heapq.heappush(yen_que, i)
snuuk_ls = [-1 for i in range(n)]
yen_ls = [-1 for i in range(n)]
yen_ls[s] = 0
snuuk_ls[t] = 0
while len(yen_que) > 0:
    a, u, v = heapq.heappop(yen_que)
    if yen_ls[v] != -1:
        continue
    yen_ls[v] = a
    for i in yen_dict[v]:
        if yen_ls[i[2]] == -1:
            heapq.heappush(yen_que, [i[0] + yen_ls[v], i[1], i[2]])
while len(snuuk_que) > 0:
    b, v, u = heapq.heappop(snuuk_que)
    if snuuk_ls[u] != -1:
        continue
    snuuk_ls[u] = b
    for i in snuuk_dict[u]:
        if snuuk_ls[i[2]] == -1:
            heapq.heappush(snuuk_que,  [i[0] + snuuk_ls[u], i[1], i[2]])
sum_ls = [10** 15 - (i + j) for i, j in zip(snuuk_ls, yen_ls)]
_max = 0
for i in range(n):
    _max = sum_ls[-1-i] if sum_ls[-1-i] > _max else _max
    sum_ls[-i-1] = _max
for i in range(n):
    print(sum_ls[i])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import permutations
n, c = [int(i) for i in sys.stdin.readline().split()]
D = dict()
C = dict()
MOD_dict = dict()
INF = 10 ** 18
for i in range(c):
    inp = [int(i) for i in sys.stdin.readline().split()]
    D[i] = inp
for i in range(n):
    inp = [int(i) - 1 for i in sys.stdin.readline().split()]
    C[i] = inp
    MOD_dict[i] = []
    for j in range(n):
        MOD_dict[i].append((i + j) % 3)
best = INF
for perm in permutations(range(c), 3):
    cost = 0
    for i in range(n):
        for j in range(n):
            pre = C[i][j]
            post = perm[MOD_dict[i][j]]
            cost += D[pre][post]
    best = min(cost, best)
print(best)

Fixed submission:
import sys
from itertools import permutations
n, c = [int(i) for i in sys.stdin.readline().split()]
D = dict()
C = dict()
MOD_dict = {i : [0 for j in range(30)] for i in range(3)}
INF = 10 ** 18
for i in range(c):
    inp = [int(i) for i in sys.stdin.readline().split()]
    D[i] = inp
for i in range(n):
    inp = [int(i) - 1 for i in sys.stdin.readline().split()]
    C[i] = inp
    for j, k in enumerate(inp):
        MOD_dict[(i+j) % 3][k] += 1
best = INF
for perm in permutations(range(c), 3):
    cost = 0
    for i in range(c):
        cost += MOD_dict[0][i] * D[i][perm[0]] +  MOD_dict[1][i] * D[i][perm[1]] + \
                MOD_dict[2][i] * D[i][perm[2]]
    best = min(cost, best)
print(best)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
start = 0
end = 1
a_ls = [bin(i)[2:].zfill(20) for i in a_ls] + ["2" * 20]
dic = {i : 0 if j == "0" else 1 for i,j in enumerate(a_ls[0])}
cnt = 0
while start < n and end <= n:
    flg = True
    a = a_ls[end]
    for i, j in enumerate(a):
        if dic[i] + int(j) > 1:
            flg = False
    if flg:
        for i, j in enumerate(a):
            dic[i] += int(j)
        end += 1
    else:
        cnt += (end - start - 1)
        for i, j in enumerate(a_ls[start]):
            dic[i] -= int(j)
        start += 1
        if end == start:
            for i, j in enumerate(a):
                dic[i] += int(j)
            end += 1
print(cnt + n)

Fixed submission:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
start = 0
end = 1
a_ls = [bin(i)[2:].zfill(20) for i in a_ls] + ["2" * 20]
dic = {i : 0 if j == "0" else 1 for i,j in enumerate(a_ls[0])}
cnt = 0
while start < n and end <= n:
    flg = True
    a = a_ls[end]
    for i, j in enumerate(a):
        if dic[i] + int(j) > 1:
            flg = False
    if flg:
        for i, j in enumerate(a):
            dic[i] += int(j)
        end += 1
    else:
        cnt += (end - start - 1)
        for i, j in enumerate(a_ls[start]):
            dic[i] -= int(j)
        start += 1
        if end == start:
            for i, j in enumerate(a):
                dic[i] += int(j)
            end += 1
print(cnt + n)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
start = 0
end = 1
a_ls = [bin(i)[2:].zfill(20) for i in a_ls] + ["2" * 20]
dic = {i : 0 if j == "0" else 1 for i,j in enumerate(a_ls[0])}
cnt = 0
while start < n and end <= n:
    flg = True
    a = a_ls[end]
    #print()
    for i, j in enumerate(a):
        dic[i] += int(j)
        if dic[i] > 1:
            flg = False
    if flg:
        end += 1
    else:
        cnt += (end - start - 1)
        for i, j in enumerate(a_ls[start]):
            dic[i] -= int(j)
        start += 1
        if end == start:
            end += 1
        else:
            for i, j in enumerate(a):
                dic[i] -= int(j)
print(cnt + n)

Fixed submission:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
start = 0
end = 1
a_ls = [bin(i)[2:].zfill(20) for i in a_ls] + ["2" * 20]
dic = {i : 0 if j == "0" else 1 for i,j in enumerate(a_ls[0])}
cnt = 0
while start < n and end <= n:
    flg = True
    a = a_ls[end]
    for i, j in enumerate(a):
        if dic[i] + int(j) > 1:
            flg = False
    if flg:
        for i, j in enumerate(a):
            dic[i] += int(j)
        end += 1
    else:
        cnt += (end - start - 1)
        for i, j in enumerate(a_ls[start]):
            dic[i] -= int(j)
        start += 1
        if end == start:
            for i, j in enumerate(a):
                dic[i] += int(j)
            end += 1
print(cnt + n)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
len_s = len(s)
already = set()
for start in range(len_s):
    for end in range(start+1, len_s+1):
        already.add(s[start:end])
candidate = sorted(list(already))
print(candidate[K-1])

Fixed submission:
s = input()
K = int(input())
len_s = len(s)
already = set()
for start in range(len_s):
    for end in range(start+1, min(start + 6, len_s+1)):
        already.add(s[start:end])
candidate = sorted(list(already))
print(candidate[K-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import bisect
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        bisect.insort_left(a_ls, (a[x], x, y))

#a_ls.sort(key=lambda x:x[0])
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    _sum = 0
    for ind in range(l, r, d):
        ind -= 1
        start = a_ls[ind]
        goal = a_ls[ind+d]
        _sum += abs(start[2] - goal[2]) + abs(start[1] - goal[1])
    print(_sum)

Fixed submission:
import sys
import bisect
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        a_ls += [(a[x], x, y)]

a_ls.sort(key=lambda x:x[0])
diff_ls = []
for ind in range(h*w):
    if ind < d:
        diff_ls.append(0)
    else:
        start = a_ls[ind-d]
        goal = a_ls[ind]
        diff = diff_ls[ind-d] + abs(start[2] - goal[2]) + abs(start[1] - goal[1])
        diff_ls.append(diff)
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    print(diff_ls[r] - diff_ls[l])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        a_ls += [(a[x], x, y)]

a_ls.sort(key=lambda x:x[0])
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    _sum = 0
    for ind in range(l, r, d):
        ind -= 1
        start = a_ls[ind]
        goal = a_ls[ind+d]
        _sum += abs(start[2] - goal[2]) + abs(start[1] - goal[1])
    print(_sum)

Fixed submission:
import sys
import bisect
h, w, d = [int(i) for i in sys.stdin.readline().split()]
a_ls = []
for y in range(h):
    a = [int(i) for i in sys.stdin.readline().split()]
    for x in range(w):
        a_ls += [(a[x], x, y)]

a_ls.sort(key=lambda x:x[0])
diff_ls = []
for ind in range(h*w):
    if ind < d:
        diff_ls.append(0)
    else:
        start = a_ls[ind-d]
        goal = a_ls[ind]
        diff = diff_ls[ind-d] + abs(start[2] - goal[2]) + abs(start[1] - goal[1])
        diff_ls.append(diff)
q = int(input())
for j in range(q):
    l, r = [int(i) for i in sys.stdin.readline().split()]
    l -= 1
    r -= 1
    print(diff_ls[r] - diff_ls[l])
Buggy python submission (verdict: Runtime Error):
H, W = [int(i) for i in input().split()]
sy, sx = [0,0]
gy, gx = [H-1, W-1]
c_ls = list()
yet_ls = [(sy,sx)]
dist_dict = {(sy, sx): 0}
road_len = 0
for i in range(H):
    c_ls.append([i for i in list(input())])
    road_len += len([i for i in c_ls[-1] if i == "."])


def dfs():
    ind = 0
    while len(yet_ls) > ind:
        y, x = yet_ls[ind]
        for move_y, move_x in [(1,0),(-1,0),(0,1),(0,-1)]:
            next_y = y + move_y
            next_x = x + move_x
            if 0 <= next_y < H and 0 <= next_x < W:
                pass
            else:
                continue
            if c_ls[next_y][next_x] == "#":
                continue
            c_ls[next_y][next_x] = "#"
            if (next_y, next_x) not in dist_dict:
                dist_dict[(next_y, next_x)] = dist_dict[(y,x)] + 1
                yet_ls.append((next_y,next_x))
            if (next_y, next_x) == (gy, gx):
                return dist_dict[(next_y, next_x)]
        ind += 1

print(road_len - dfs() - 1)

Fixed submission:
H, W = [int(i) for i in input().split()]
sy, sx = [0,0]
gy, gx = [H-1, W-1]
c_ls = list()
yet_ls = [(sy,sx)]
dist_dict = {(sy, sx): 0}
road_len = 0
for i in range(H):
    c_ls.append([i for i in list(input())])
    road_len += len([i for i in c_ls[-1] if i == "."])


def dfs():
    ind = 0
    while len(yet_ls) > ind:
        y, x = yet_ls[ind]
        for move_y, move_x in [(1,0),(-1,0),(0,1),(0,-1)]:
            next_y = y + move_y
            next_x = x + move_x
            if 0 <= next_y < H and 0 <= next_x < W:
                pass
            else:
                continue
            if c_ls[next_y][next_x] == "#":
                continue
            c_ls[next_y][next_x] = "#"
            if (next_y, next_x) not in dist_dict:
                dist_dict[(next_y, next_x)] = dist_dict[(y,x)] + 1
                yet_ls.append((next_y,next_x))
            if (next_y, next_x) == (gy, gx):
                return dist_dict[(next_y, next_x)]
        ind += 1
    return road_len


print(road_len - dfs() - 1)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import re
s = input()
x, y = [int(i) for i in sys.stdin.readline().split()]
s = [len(i) for i in re.split("T", s)]
v_ls = s[1::2]
h_ls = s[::2]
x -= h_ls[0]
h_ls.pop(0)
v_sum = sum(v_ls)
h_sum = sum(h_ls)
que = [(0, 0)]
# memo_v_ls[i][j] : i番目まででj - v_sumができるか
width = max(abs(y), v_sum)
memo_v_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(v_ls) + 1)]
memo_v_ls[0][width] = 1
for j, v in enumerate(v_ls, 1):
    for i in range(width * 2 + 1):
        if i >= v and i <= v_sum - v:
            memo_v_ls[j][i] = max(memo_v_ls[j-1][i-v], memo_v_ls[j-1][i], memo_v_ls[j-1][i+v])
        elif i >= v:
            memo_v_ls[j][i] = max(memo_v_ls[j - 1][i - v], memo_v_ls[j - 1][i])
        elif i <= v_sum - v:
            memo_v_ls[j][i] = max(memo_v_ls[j - 1][i - v], memo_v_ls[j - 1][i + v])
        else:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i]

flg = memo_v_ls[-1][y + width] == 1

width = max(abs(x), h_sum)
memo_h_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(h_ls) + 1)]
memo_h_ls[0][width] = 1
for j, h in enumerate(h_ls, 1):
    for i in range(width * 2 + 1):
        if i >= h and i <= h_sum - h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i], memo_h_ls[j - 1][i + h])
        elif i >= h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i])
        elif i <= h_sum - h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i + h])
        else:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i]

flg &= memo_h_ls[-1][x + width] == 1
if flg:
    print("Yes")
else:
    print("No")

Fixed submission:
import sys
import re
s = input()
x, y = [int(i) for i in sys.stdin.readline().split()]
s = [len(i) for i in re.split("T", s)]
v_ls = s[1::2]
h_ls = s[::2]
x -= h_ls[0]
h_ls.pop(0)
v_sum = sum(v_ls)
h_sum = sum(h_ls)
# memo_v_ls[i][j] : i番目まででj - v_sumができるか
width = max(abs(y), v_sum)
memo_v_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(v_ls) + 1)]
memo_v_ls[0][width] = 1
for j, v in enumerate(v_ls, 1):
    for i in range(width * 2 + 1):
        if i >= v and i <= 2 * width - v:
            memo_v_ls[j][i] = max(memo_v_ls[j - 1][i - v], memo_v_ls[j - 1][i + v])
        elif i >= v:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i - v]
        else:
            memo_v_ls[j][i] = memo_v_ls[j - 1][i + v]

flg = memo_v_ls[-1][y + width] == 1

width = max(abs(x), h_sum)
memo_h_ls = [[0 for i in range(width * 2 + 1)] for j in range(len(h_ls) + 1)]
memo_h_ls[0][width] = 1
for j, h in enumerate(h_ls, 1):
    for i in range(width * 2 + 1):
        if i >= h and i <= 2 * width - h:
            memo_h_ls[j][i] = max(memo_h_ls[j - 1][i - h], memo_h_ls[j - 1][i + h])
        elif i >= h:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i - h]
        else:
            memo_h_ls[j][i] = memo_h_ls[j - 1][i + h]

flg &= memo_h_ls[-1][x + width] == 1
if flg:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
import sys
from itertools import accumulate
n, c = [int(i) for i in sys.stdin.readline().split()]
ls = [0 for i in range(10**5+1)]
for i in range(n):
    s, t, c = [int(i) for i in sys.stdin.readline().split()]
    ls[s] += 1
    ls[t+1] -= 1
ls = [i for i in accumulate(ls)]
print(max(ls))

Fixed submission:
import sys
from itertools import accumulate
N, C = [int(i) for i in sys.stdin.readline().split()]
ls = {j : [0 for i in range(2*10**5)] for j in range(1,C+1)}
for i in range(N):
    s, t, c = [int(i) for i in sys.stdin.readline().split()]
    ls[c][s] += 1
    ls[c][t+1] -= 1

res = [0 for i in range(2*10**5)]
for _c in range(1, C+1):
    for i, j in enumerate(accumulate(ls[_c])):
        res[i] += min(1,j)
print(max(res))