Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

S = tuple(map(int, list(input())))
MOD = 2019
dp = [defaultdict(int) for _ in range(len(S) + 1)]
for i in range(len(S)):
    dp[i + 1][S[i]] += 1
    for j in dp[i]:
        dp[i + 1][(j * 10 + S[i]) % MOD] += dp[i][j]

print(sum(dp[i][0] for i in range(len(S) + 1)))

Fixed submission:
from collections import defaultdict

S = tuple(map(int, list(input())))
MOD = 2019

d = defaultdict(int)
d[0] = 1

tmp = 0
for i, s in enumerate(reversed(S)):
    tmp = (tmp + s * pow(10, i, MOD)) % MOD
    d[tmp] += 1
print(sum(v * (v - 1) // 2 for v in d.values()))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

S = tuple(map(int, list(input())))
MOD = 2019
dp = [defaultdict(int) for _ in range(len(S) + 1)]
for i in range(len(S)):
    dp[i + 1][S[i]] += 1
    for j in range(MOD):
        dp[i + 1][(j * 10 + S[i]) % MOD] += dp[i][j]

print(sum(dp[i][0] for i in range(len(S) + 1)))

Fixed submission:
from collections import defaultdict

S = tuple(map(int, list(input())))
MOD = 2019

d = defaultdict(int)
d[0] = 1

tmp = 0
for i, s in enumerate(reversed(S)):
    tmp = (tmp + s * pow(10, i, MOD)) % MOD
    d[tmp] += 1
print(sum(v * (v - 1) // 2 for v in d.values()))
Buggy python submission (verdict: Time Limit Exceeded):
import itertools
from sys import exit

N = int(input())
S = input()
if N < 3:
    print(0)
    exit()

d = {'R': 0, 'G': 1, 'B': 2}
rgb = {'R', 'G', 'B'}

table = [[0] * N for _ in range(3)]
for i, s in enumerate(S):
    table[d[s]][i] = 1

for x in range(3):
    table[x] = tuple(itertools.accumulate(table[x]))

ans = 0
for k in range(2, N):
    for j in range(1, k):
        if S[j] != S[k] :
            x = (rgb - {S[j], S[k]}).pop()
            ans += table[d[x]][j - 1]
            if 2 * j - k >= 0 and S[2 * j - k] == x:
                ans -= 1
print(ans)

Fixed submission:
import itertools
from sys import exit

N = int(input())
S = input()
if N < 3:
    print(0)
    exit()

d = {'R': 0, 'G': 1, 'B': 2}
rgb = {'R', 'G', 'B'}

table = [[0] * N for _ in range(3)]
for i, s in enumerate(S):
    table[d[s]][i] = 1

for x in range(3):
    table[x] = tuple(itertools.accumulate(table[x]))

ans = 0
for k in range(2, N):
    for j in range(1, k):
        if S[j] != S[k] :
            x = (rgb - {S[j], S[k]}).pop()
            ans += table[d[x]][j - 1]
            if 2 * j - k >= 0 and S[2 * j - k] == x:
                ans -= 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())


def make_divisors(n):
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            yield i
            if i != n // i:
                yield n // i


ans = len(tuple(make_divisors(N - 1))) - 1

it = make_divisors(N)
next(it)

for k in it:
    tmp = N
    while not tmp % k:
        tmp //= k
    if tmp % k == 1:
        ans += 1
print(ans)


Fixed submission:
X, Y, Z = map(int, input().split())
print(Z, X, Y)
Buggy python submission (verdict: Runtime Error):
import itertools


N, X, Y = map(int, input().split())
lst = [0] * N
for l,r in itertools.combinations(range(1,N+1),2):
    lst[min(r-l,abs(r-Y)+1+abs(l-X))] += 1
for i in range(1,N+1):
    print(lst[i])

Fixed submission:
import itertools

N, X, Y = map(int, input().split())
lst = [0] * N
for l, r in itertools.combinations(range(1, N + 1), 2):
    lst[min(r - l, abs(r - Y) + 1 + abs(l - X))] += 1
for i in range(1, N):
    print(lst[i])
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import itertools

H, W, K = map(int, input().split())
S = np.array([[int(s) for s in input()] for _ in range(H)])

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cut = t.count(1)
    lst = np.zeros((cut + 1, W), dtype=np.int)
    lst[0] += S[0]
    c = 0
    for h in range(H - 1):
        if t[h]:
            c += 1
        lst[c] += S[h + 1]
    L = len(lst)
    sum_lst = np.zeros(L)
    for w in range(W):
        lst_w = lst[:, w]
        if lst_w.max() > K:
            break

        lst_s = sum_lst + lst_w
        if lst_s.max() > K:
            cut += 1
            sum_lst = lst_w
        else:
            sum_lst = lst_s
    else:
        ans = min(ans, cut)
print(ans)


Fixed submission:
import itertools

H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append([int(s) for s in input()])

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
import itertools

H, W, K = map(int, input().split())
S = np.array([[int(s) for s in input()] for _ in range(H)])

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = np.copy(S[0]).reshape(1, -1)
    for h in range(H - 1):
        if t[h]:
            lst = np.vstack([lst, S[h + 1]])
        else:
            lst[-1] = lst[-1] + S[h + 1]
    L = len(lst)
    sum_lst = np.zeros(L)
    for w in range(W):
        lst_w = lst[:, w]
        if lst_w.max() > K:
            break
        lst_s = sum_lst + lst_w
        if lst_s.max() > K:
            cnt += 1
            sum_lst = lst_w
        else:
            sum_lst = lst_s
    else:
        ans = min(ans, cnt)
print(ans)


Fixed submission:
import itertools

H, W, K = map(int, input().split())
S = []
for _ in range(H):
    S.append([int(s) for s in input()])

ans = 2000
for t in itertools.product([0, 1], repeat=H - 1):
    cnt = t.count(1)
    lst = [[s for s in S[0]]]
    for h in range(H - 1):
        if t[h]:
            lst.append(S[h + 1])
        else:
            lst[-1] = [lst[-1][i] + S[h + 1][i] for i in range(W)]
    L = len(lst)
    sum_lst = [0] * L
    for w in range(W):
        if max(lst[i][w] for i in range(L)) > K:
            break
        tmp = [sum_lst[i] + lst[i][w] for i in range(L)]
        if max(tmp) > K:
            cnt += 1
            sum_lst = [lst[i][w] for i in range(L)]
        else:
            sum_lst = tmp
    else:
        ans = min(ans, cnt)
print(ans)
Buggy python submission (verdict: Runtime Error):
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

table = [[None] * W for _ in range(H)]
table[0][0] = (0 if S[0][0] == '.' else 1)

D = deque([(0, 0)])
new_D = deque()
while D:
    h, w = D.popleft()
    for dh, dw in ((1, 0), (0, 1)):
        if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None:
            if S[h + dh][w + dw] == S[h][w]:
                D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w]
            else:
                new_D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w] + 1
    if not D:
        D = new_D.copy()
        new_D.clear()

print((table[H - 1][W - 1] + 1) // 2)


Fixed submission:
from copy import deepcopy
from collections import deque

H, W = map(int, input().split())
S = [input() for _ in range(H)]

table = [[None] * W for _ in range(H)]
table[0][0] = (0 if S[0][0] == '.' else 1)

D = deque([(0, 0)])
new_D = deque()
while D:
    h, w = D.popleft()
    for dh, dw in ((1, 0), (0, 1)):
        if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None:
            if S[h + dh][w + dw] == S[h][w]:
                D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w]
            else:
                new_D.append((h + dh, w + dw))
                table[h + dh][w + dw] = table[h][w] + 1
    if not D:
        D = deepcopy(new_D)
        new_D.clear()

print((table[H - 1][W - 1] + 1) // 2)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
import numpy as np
from scipy.sparse.csgraph import connected_components
from scipy.sparse import csr_matrix

N, M, K = map(int, input().split())
exc = [set() for _ in range(N)]
edge = []

for _ in range(M):
    a, b = map(int, input().split())
    exc[a - 1].add(b - 1)
    exc[b - 1].add(a - 1)
    edge.append((a - 1, b - 1))

edge = np.array(edge).T
matr = csr_matrix(([1 for _ in range(M)], edge), (N, N))
_, labels = connected_components(matr)
ct = Counter(labels)

for _ in range(K):
    c, d = map(int, input().split())
    if labels[c - 1] == labels[d - 1]:
        exc[c - 1].add(d - 1)
        exc[d - 1].add(c - 1)

ans = [ct[labels[i]] - len(exc[i]) - 1 for i in range(N)]
print(*ans)


Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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

    def group_size(self, x):
        return self.size[self.find(x)]


N, M, K = map(int, input().split())
uf = UnionFind(N)
exc = [set() for _ in range(N)]

for _ in range(M):
    a, b = map(int, input().split())
    exc[a - 1].add(b - 1)
    exc[b - 1].add(a - 1)
    uf.unite(a - 1, b - 1)
for _ in range(K):
    c, d = map(int, input().split())
    if uf.issame(c - 1, d - 1):
        exc[c - 1].add(d - 1)
        exc[d - 1].add(c - 1)

ans = [uf.group_size(i) - len(exc[i]) - 1 for i in range(N)]
print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input())
Q = int(input())


class Bit:
    """1-indexed"""

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


en2asc = lambda s: ord(s) - 97

Bits = [Bit(N) for _ in range(26)]
for i, s in enumerate(S):
    Bits[en2asc(s)].add(i + 1, 1)

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        i, c = int(q[1]), q[2]
        old = S[i - 1]
        Bits[en2asc(old)].add(i, -1)
        Bits[en2asc(c)].add(i, 1)
        S[i - 1] = c
    else:
        l, r = int(q[1]), int(q[2])
        ans = 0
        for b in Bits:
            ans += bool(b.sum(r) - b.sum(l - 1))
        print(ans)


Fixed submission:
N = int(input())
S = list(input())
Q = int(input())


class Bit:
    """1-indexed"""

    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


en2asc = lambda s: ord(s) - 97

Bits = [Bit(N) for _ in range(26)]
for i, s in enumerate(S):
    Bits[en2asc(s)].add(i + 1, 1)

for _ in range(Q):
    q = input().split()
    if q[0] == '1':
        i, c = int(q[1]), q[2]
        old = S[i - 1]
        Bits[en2asc(old)].add(i, -1)
        Bits[en2asc(c)].add(i, 1)
        S[i - 1] = c
    else:
        l, r = int(q[1]), int(q[2])
        ans = 0
        for b in Bits:
            ans += bool(b.sum(r) - b.sum(l - 1))
        print(ans)
Buggy python submission (verdict: Runtime Error):
r1, c1, r2, c2 = map(int, input().split())

MOD = 10 ** 9 + 7
MAX = 10 ** 6 + 5
modinv = lambda a, mod=MOD: pow(a, mod - 2, mod)
fac, inv = [1] * MAX, [1] * MAX
for i in range(1, MAX):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = modinv(fac[i])


def comb(n, k, error=0):
    if not 0 <= k <= n:
        return error
    return fac[n] * inv[k] * inv[n - k] % MOD


ans = comb(r2 + c2 + 2, r2 + 1) + comb(r1 + c1, r1) - comb(r1 + c2 + 1, r1) - comb(r2 + c1 + 1, c1)
ans %= MOD
print(ans)

Fixed submission:
r1, c1, r2, c2 = map(int, input().split())

MOD = 10 ** 9 + 7
MAX = 2 * 10 ** 6 + 10
modinv = lambda a, mod=MOD: pow(a, mod - 2, mod)
fac, inv = [1] * MAX, [1] * MAX
for i in range(1, MAX):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = modinv(fac[i])


def comb(n, k, error=0):
    if not 0 <= k <= n:
        return error
    return fac[n] * inv[k] * inv[n - k] % MOD


ans = comb(r2 + c2 + 2, r2 + 1) + comb(r1 + c1, r1) - comb(r1 + c2 + 1, r1) - comb(r2 + c1 + 1, c1)
ans %= MOD
print(ans)

Buggy python submission (verdict: Runtime Error):
r1, c1, r2, c2 = map(int, input().split())

MOD = 10 ** 9 + 7
MAX = 10 ** 6 + 5
modinv = lambda a, mod=MOD: pow(a, -1, mod)
fac, inv = [1] * MAX, [1] * MAX
for i in range(1, MAX):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = modinv(fac[i])


def comb(n, k, error=0):
    if not 0 <= k <= n:
        return error
    return fac[n] * inv[k] * inv[n - k] % MOD


ans = comb(r2 + c2 + 2, r2 + 1) + comb(r1 + c1, r1) - comb(r1 + c2 + 1, r1) - comb(r2 + c1 + 1, c1)
ans %= MOD
print(ans)

Fixed submission:
r1, c1, r2, c2 = map(int, input().split())

MOD = 10 ** 9 + 7
MAX = 2 * 10 ** 6 + 10
modinv = lambda a, mod=MOD: pow(a, mod - 2, mod)
fac, inv = [1] * MAX, [1] * MAX
for i in range(1, MAX):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = modinv(fac[i])


def comb(n, k, error=0):
    if not 0 <= k <= n:
        return error
    return fac[n] * inv[k] * inv[n - k] % MOD


ans = comb(r2 + c2 + 2, r2 + 1) + comb(r1 + c1, r1) - comb(r1 + c2 + 1, r1) - comb(r2 + c1 + 1, c1)
ans %= MOD
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
H, N = map(int, input().split())
AB = []
for _ in range(N):
    AB.append(tuple(map(int, input().split())))
max_a = max(ab[0] for ab in AB)
table = [0] + [float('inf')] * (H + max_a)

for i in range(N):
    for j in range(AB[i][0], H + max_a):
        table[j] = min(table[j], table[j - AB[i][0]] + AB[i][1])
print(min(table[H:]))


Fixed submission:
H, N = map(int, input().split())
AB = []
for _ in range(N):
    AB.append(tuple(map(int, input().split())))
max_a = max(ab[0] for ab in AB)
table = [0] + [float('inf')] * (H + max_a)

for i in range(N):
    for j in range(AB[i][0], H + max_a):
        table[j] = min(table[j], table[j - AB[i][0]] + AB[i][1])
print(min(table[H:]))
Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
# 逆元なしでいけるか確認

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7


lcm = A[0]
for i in range(1, N):
    lcm = (lcm * A[i] // (gcd(lcm, A[i])))
ans = 0

for a in A:
    ans = (ans + lcm // a) % MOD

print(ans)

Fixed submission:
from fractions import gcd

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7


def modinv(a, mod=10 ** 9 + 7):
    return pow(a, mod - 2, mod)

lcm = A[0]
for i in range(1, N):
    lcm = (lcm * A[i] // (gcd(lcm, A[i])))
ans = 0
lcm %= MOD

for a in A:
    ans = (ans + lcm * modinv(a)) % MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from fractions import gcd
# 工夫せずにやって通るか確認

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7


lcm = A[0]
for i in range(1, N):
    lcm = (lcm * A[i] // (gcd(lcm, A[i])))
ans = 0

for a in A:
    ans = (ans + lcm // a) % MOD

print(ans)

Fixed submission:
from fractions import gcd

N = int(input())
A = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7


def modinv(a, mod=10 ** 9 + 7):
    return pow(a, mod - 2, mod)

lcm = A[0]
for i in range(1, N):
    lcm = (lcm * A[i] // (gcd(lcm, A[i])))
ans = 0
lcm %= MOD

for a in A:
    ans = (ans + lcm * modinv(a)) % MOD

print(ans)
Buggy python submission (verdict: Runtime Error):
N, K, S = map(int, input().split())

if S != 10 ** 9:
    print(*[S] * K, *[10 ** 9] * (N - K))
else:
    print(*[10 ** 9] * K, *[1] * (N - K))


Fixed submission:
N, K, S = map(int, input().split())

if S != 10 ** 9:
    print(' '.join(map(str, [S] * K)),' '.join(map(str, [10 ** 9] * (N - K))))
else:
    print(' '.join(map(str, [S] * K)), ' '.join(map(str, [1] * (N - K))))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
B = []
for x in A:
    tmp_lst = []
    for _ in range(60):
        tmp = x // 2
        tmp_lst.append(x - tmp * 2)
        x = tmp
    B.append(tmp_lst)

ans = 0
for i in range(60):
    tr, fl = 0, 0
    for x in B:
        if x[i]:
            tr += 1
        else:
            fl += 1
    ans += (tr * fl) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = []
for x in A:
    tmp_lst = []
    for _ in range(60):
        tmp = x // 2
        tmp_lst.append(x - tmp * 2)
        x = tmp
    B.append(tmp_lst)

ans = 0
for i in range(60):
    tr, fl = 0, 0
    for x in B:
        if x[i]:
            tr += 1
        else:
            fl += 1
    ans += (tr * fl) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
for i in range(60):
    tr, fl = 0, 0
    for x in A:
        if x >> i & 1:
            tr += 1
        else:
            fl += 1
    ans += (tr * fl) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = []
for x in A:
    tmp_lst = []
    for _ in range(60):
        tmp = x // 2
        tmp_lst.append(x - tmp * 2)
        x = tmp
    B.append(tmp_lst)

ans = 0
for i in range(60):
    tr, fl = 0, 0
    for x in B:
        if x[i]:
            tr += 1
        else:
            fl += 1
    ans += (tr * fl) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
for i in range(60):
    tmp = [x >> i & 1 for x in A]
    ans += (tmp.count(True) * tmp.count(False)) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)


Fixed submission:
N = int(input())
A = [int(x) for x in input().split()]
B = []
for x in A:
    tmp_lst = []
    for _ in range(60):
        tmp = x // 2
        tmp_lst.append(x - tmp * 2)
        x = tmp
    B.append(tmp_lst)

ans = 0
for i in range(60):
    tr, fl = 0, 0
    for x in B:
        if x[i]:
            tr += 1
        else:
            fl += 1
    ans += (tr * fl) * 2 ** i
    ans = ans % (10 ** 9 + 7)
print(ans)
Buggy python submission (verdict: Runtime Error):
import numpy as np
import itertools

H, W = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
table = np.zeros((H, W, 80 * (H + W)), dtype='bool')
table[0][0][D[0][0]] = True

it = itertools.product(range(H), range(W))
next(it)  # h=w=0 捨てる
for h, w in it:
    d = D[h][w]
    table[h][w][d:] += table[h - 1][w][:-d] + table[h][w - 1][:-d]
    table[h][w][:-d] += table[h - 1][w][d:] + table[h][w - 1][d:]
    table[h][w][d:0:-1] += table[h - 1][w][:d] + table[h][w - 1][:d]

print(np.amin(*np.where(table[H-1][W-1])))


Fixed submission:
import numpy as np
import itertools

H, W = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
max_num = 80 * (H + W)
table = np.zeros((H, W, max_num), dtype='bool')
table[0][0][D[0][0]] = True

it = itertools.product(range(H), range(W))
next(it)  # h=w=0 捨てる
for h, w in it:
    d = D[h][w]
    table[h][w][d:] += table[h - 1][w][:max_num - d] + table[h][w - 1][:max_num - d]
    table[h][w][:max_num - d] += table[h - 1][w][d:] + table[h][w - 1][d:]
    table[h][w][d:0:-1] += table[h - 1][w][:d] + table[h][w - 1][:d]

print(np.amin(*np.where(table[H - 1][W - 1])))

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
table = [[set() for __ in range(W)] for _ in range(H)]
table[0][0] = {D[0][0]}

for h in range(H):
    for w in range(W):
        Dhw = D[h][w]
        table[h][w] |= {x + Dhw for x in table[h - 1][w]} | {abs(x - Dhw) for x in table[h - 1][w]} | \
                       {x + Dhw for x in table[h][w - 1]} | {abs(x - Dhw) for x in table[h][w - 1]}

print(min(table[H - 1][W - 1]))


Fixed submission:
import numpy as np
import itertools

H, W = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
max_num = 80 * (H + W)
table = np.zeros((H, W, max_num), dtype='bool')
table[0][0][D[0][0]] = True

it = itertools.product(range(H), range(W))
next(it)  # h=w=0 捨てる
for h, w in it:
    d = D[h][w]
    table[h][w][d:] += table[h - 1][w][:max_num - d] + table[h][w - 1][:max_num - d]
    table[h][w][:max_num - d] += table[h - 1][w][d:] + table[h][w - 1][d:]
    table[h][w][d:0:-1] += table[h - 1][w][:d] + table[h][w - 1][:d]

print(np.amin(*np.where(table[H - 1][W - 1])))
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()

ans = 0
for num in range(1000):
    target = str(num).zfill(3)
    cur = 0
    for s in S:
        if s == target[cur]:
            cur += 1
        if cur == 3:
            ans += 1
            break
print(ans)


Fixed submission:
N = int(input())
S = input()

ans = 0
for num in range(1000):
    target = str(num).zfill(3)
    cur = 0
    for s in S:
        if s == target[cur]:
            cur += 1
        if cur == 3:
            ans += 1
            break
print(ans)
Buggy python submission (verdict: Runtime Error):
import sys

X, Y = map(int, input().split())

# 直交座標に直す
if (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
    print(0)
    sys.exit()
x, y = (2 * X - Y) // 3, (2 * Y - X) // 3


def comb_inv(n, k, mod):
    f = [1 for _ in range(n + 1)]
    inv = [1 for _ in range(n + 1)]
    finv = [1 for _ in range(n + 1)]

    for i in range(2, n + 1):
        f[i] = f[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod
    return f[n] * (finv[k] * finv[n - k] % mod) % mod


print(comb_inv(x + y, x, 10 ** 9 + 7))


Fixed submission:
import sys

X, Y = map(int, input().split())

# 直交座標に直す
if X > 2 * Y or Y > 2 * X or \
        (2 * X - Y) % 3 != 0 or (2 * Y - X) % 3 != 0:
    print(0)
    sys.exit()
x, y = (2 * X - Y) // 3, (2 * Y - X) // 3


def comb_inv(n, k, mod):
    f = [1 for _ in range(n + 1)]
    inv = [1 for _ in range(n + 1)]
    finv = [1 for _ in range(n + 1)]

    for i in range(2, n + 1):
        f[i] = f[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod
    return f[n] * (finv[k] * finv[n - k] % mod) % mod


print(comb_inv(x + y, x, 10 ** 9 + 7))