Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
def d_moving_piece():
    N, K = [int(i) for i in input().split()]
    P = [int(i) - 1 for i in input().split()]  # マスの番号は 0-indexed とする
    C = [int(i) for i in input().split()]

    # 順列をサイクルごとに分解
    is_visited = [False] * N
    cycles = []  # サイクルごとのスコアを記録
    for i in range(N):
        if is_visited[i]:
            continue

        current = i
        t = []
        while not is_visited[current]:
            is_visited[current] = True
            t.append(C[current])
            current = P[current]
        cycles.append(t)

    ans = -float('inf')
    for s in cycles:
        m = len(s)

        # サイクルを 2 周したものの累積和
        total = [0] * (2 * m + 1)
        for i in range(2 * m):
            total[i + 1] = total[i] + s[i % m]

        reminder = [-float('inf')] * m  # [r]: 連続する r 個の和の最大値
        for i in range(m):
            for j in range(m):
                reminder[j] = max(reminder[j], total[i + j] - total[i])

        # 「余り」の長さで場合分け
        for r in range(m):
            if r > K:
                continue  # 移動できる回数より多く動いたことになり、矛盾

            q = (K - r) // m  # この回数だけサイクルを 1 周する
            if r == 0 and q == 0:
                continue  # 動いていないので、除外

            if total[m] > 0:
                # 周回したほうが得なので、可能な限り周回する
                ans = max(ans, reminder[r] + total[m] * q)
            elif r > 0:
                # 周回すると損なので、必要なだけ (r 回) 移動したら終わる
                ans = max(ans, reminder[r])
    return ans

print(d_moving_piece())

Fixed submission:
def d_moving_piece():
    N, K = [int(i) for i in input().split()]
    P = [int(i) - 1 for i in input().split()]  # マスの番号は 0-indexed とする
    C = [int(i) for i in input().split()]

    # 順列をサイクルごとに分解
    is_visited = [False] * N
    cycles = []  # サイクルごとのスコアを記録
    for i in range(N):
        if is_visited[i]:
            continue

        current = i
        t = []
        while not is_visited[current]:
            is_visited[current] = True
            t.append(C[current])
            current = P[current]
        cycles.append(t)

    ans = -float('inf')
    for s in cycles:
        m = len(s)

        # サイクルを 2 周したものの累積和
        total = [0] * (2 * m + 1)
        for i in range(2 * m):
            total[i + 1] = total[i] + s[i % m]

        reminder = [-float('inf')] * m  # [r]: 連続する r 個の和の最大値
        for i in range(m):
            for j in range(m):
                reminder[j] = max(reminder[j], total[i + j] - total[i])

        # 「余り」の長さで場合分け
        for r in range(m):
            if r > K:
                continue  # 移動できる回数より多く動いたことになり、矛盾

            q = (K - r) // m  # この回数だけサイクルを 1 周する
            if r == 0 and q == 0:
                continue  # 動いていないので、除外

            if total[m] > 0:
                # 周回したほうが得なので、可能な限り周回する
                ans = max(ans, reminder[r] + total[m] * q)
            elif r > 0:
                # 周回すると損なので、必要なだけ (r 回) 移動したら終わる
                ans = max(ans, reminder[r])
    return ans

print(d_moving_piece())
Buggy python submission (verdict: Time Limit Exceeded):
def e_picking_goods():
    R, C, K = [int(i) for i in input().split()]
    Goods = [[0] * (C + 1) for _ in range(R + 1)]
    for _ in range(K):
        r, c, v = [int(i) for i in input().split()]
        Goods[r][c] = v

    # dp[i][j][k]: マス (i, j) まで来たとき、その時点で i 行目のアイテムは k 個回収している
    # 場合の、回収したアイテムの価値の総和の最大値
    dp = [[[0] * 4 for j in range(C + 1)] for i in range(R + 1)]
    for i in range(R + 1):
        for j in range(C + 1):
            for k in range(4):
                # 下へ行く
                if i - 1 >= 0:
                    # 行を変えるので、k は 0 にリセットされる
                    dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k])  # (i, j) のアイテムを拾わない
                    dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][k] + Goods[i][j])  # 拾う

                # 右へ行く
                if j - 1 >= 0:
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k])  # 拾わない
                    if k - 1 >= 0:
                        dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + Goods[i][j])  # まだ拾えるし、拾う
    return max(dp[R][C][k] for k in range(4))

print(e_picking_goods())

Fixed submission:
def e_picking_goods():
    # 参考: https://atcoder.jp/contests/abc175/submissions/16044254
    R, C, K = [int(i) for i in input().split()]
    Goods = [[0] * (C + 1) for _ in range(R + 1)]
    for _ in range(K):
        r, c, v = [int(i) for i in input().split()]
        Goods[r][c] = v

    # editorial に対し、行の情報を除去する
    dp = [[0] * 4 for _ in range(C + 1)]
    for i in range(1, R + 1):
        dp_next = [[0] * 4 for _ in range(C + 1)]
        for j in range(1, C + 1):
            up_max = max(dp[j])  # 上のマスで 0~3 回アイテムを拾っていたときの、価値の最大値
            v = Goods[i][j]

            # 左か上から来て、アイテムを拾わない
            dp_next[j][0] = max(up_max, dp_next[j - 1][0])
            # 上から来て拾う or 左から来て拾う or 左から来て拾わない
            dp_next[j][1] = max(up_max + v, dp_next[j - 1][0] + v, dp_next[j - 1][1])
            # 左から来て k-1 個拾っており、ここで拾う or k 個拾っており、ここで拾わない
            for k in range(2, 4):
                dp_next[j][k] = max(dp_next[j - 1][k - 1] + v, dp_next[j - 1][k])
        dp = dp_next  # 次の行へ注目を移す
    return max(dp[-1])

print(e_picking_goods())
Buggy python submission (verdict: Runtime Error):
print(8 - (X // 200 - 2))

Fixed submission:
print(8 - (int(input()) // 200 - 2))
Buggy python submission (verdict: Time Limit Exceeded):
def c_marks():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    score = 1
    for a in A[:K]:
        score *= a

    ans = ['Yes' if A[i + K] > A[i] else 'No' for i in range(N - K)]
    return ' '.join(ans)

print(c_marks())

Fixed submission:
def c_marks():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    ans = ['Yes' if A[i + K] > A[i] else 'No' for i in range(N - K)]
    return '\n'.join(ans)

print(c_marks())

Buggy python submission (verdict: Time Limit Exceeded):
def c_marks():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    score = 1
    for a in A[:K]:
        score *= a

    ans = []
    for i in range(N - K):
        score_before = score
        score //= A[i]
        score *= A[i + K]
        ans.append('Yes' if score > score_before else 'No')
    return ' '.join(ans)

print(c_marks())

Fixed submission:
def c_marks():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    ans = ['Yes' if A[i + K] > A[i] else 'No' for i in range(N - K)]
    return '\n'.join(ans)

print(c_marks())
Buggy python submission (verdict: Runtime Error):
def e_multiplication_4(MOD=10**9 + 7):
    from functools import reduce
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    sign = {-1: 0, 0: 0, 1: 0}  # A の各要素の符号
    for a in A:
        sign[a // abs(a)] += 1

    if sign[1] == 0 and K % 2 == 1:
        # どうやっても解は負。絶対値の小さなものから掛けていく
        A.sort(reverse=True)
        return reduce(lambda x, y: (x * y) % MOD, A[:K])
    if sign[1] == 0 and K % 2 == 0:
        # 先程と違って絶対値の大きなものから掛けても解は正になる
        A.sort()
        return reduce(lambda x, y: (x * y) % MOD, A[:K])

    A.sort()
    ans = 1
    i, j = 0, N - 1
    while K > 0:
        # 負の絶対値が大きいものから 2 個取るか？　正の絶対値が大きいものから 1 個取るか？
        # i, j がそれぞれ負の値、非負の値を指さなくなった場合、if 文のどちらかを
        # 確実に通らなくなるので、i, j についての条件は必要ない
        if K > 1 and A[i] * A[i + 1] >= A[j] * A[j - 1]:
            ans *= A[i] * A[i + 1]
            i += 2
            K -= 2
        else:
            ans *= A[j]
            j -= 1
            K -= 1
        ans %= MOD
    return ans

print(e_multiplication_4())

Fixed submission:
def e_multiplication_4(MOD=10**9 + 7):
    from functools import reduce
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    sign = {-1: 0, 0: 0, 1: 0}  # A の各要素の符号
    for a in A:
        if a == 0:
            sign[0] += 1
            continue
        sign[a // abs(a)] += 1

    if sign[1] == 0 and K % 2 == 1:
        # どうやっても解は負。絶対値の小さなものから掛けていく
        A.sort(reverse=True)
        return reduce(lambda x, y: (x * y) % MOD, A[:K])
    if sign[1] == 0 and K % 2 == 0:
        # 先程と違って絶対値の大きなものから掛けても解は正になる
        A.sort()
        return reduce(lambda x, y: (x * y) % MOD, A[:K])

    A.sort()
    ans = 1
    i, j = 0, N - 1
    while K > 0:
        # 負の絶対値が大きいものから 2 個取るか？　正の絶対値が大きいものから 1 個取るか？
        # i, j がそれぞれ負の値、非負の値を指さなくなった場合、if 文のどちらかを
        # 確実に通らなくなるので、i, j についての条件は必要ない
        if K > 1 and A[i] * A[i + 1] >= A[j] * A[j - 1]:
            ans *= A[i] * A[i + 1]
            i += 2
            K -= 2
        else:
            ans *= A[j]
            j -= 1
            K -= 1
        ans %= MOD
    return ans

print(e_multiplication_4())
Buggy python submission (verdict: Runtime Error):
A, V = [int(i) for i in input().split()]
B, W = [int(i) for i in input().split()]

print('YES' if V > W and abs(A - B) <= T * (V - W) else 'NO')

Fixed submission:
A, V = [int(i) for i in input().split()]
B, W = [int(i) for i in input().split()]
T = int(input())

print('YES' if V > W and abs(A - B) <= T * (V - W) else 'NO')
Buggy python submission (verdict: Time Limit Exceeded):
from operator import mul
from functools import reduce
N = int(input())
A = [int(i) for i in input().split()]
ans = reduce(mul, A)
print(ans if ans <= 10**18 else -1)

Fixed submission:
N = int(input())
A = sorted([int(i) for i in input().split()])
ans = 1
for a in A:
    if a == 0:
        ans = 0
        break
    ans *= a
    if ans > 10**18:
        ans = -1
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [int(i) for i in input().split()]
ans = reduce(mul, A)
print(ans if ans <= 10**18 else -1)

Fixed submission:
N = int(input())
A = sorted([int(i) for i in input().split()])
ans = 1
for a in A:
    if a == 0:
        ans = 0
        break
    ans *= a
    if ans > 10**18:
        ans = -1
        break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def f_knapsack_for_all_subsets(MOD=998244353):
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    # dp[i][j]: i 番目の要素を見たとき、和が j であるような場合の数
    dp = [[0 for j in range(S + 1)] for i in range(N + 1)]
    dp[0][0] = 1  # インデックスの集合が空のとき、和は 0
    for i, a in enumerate(A):
        for j in range(S + 1):
            # editorial でいうところの選択肢 2, 3 に相当するので、2 倍
            dp[i + 1][j] += 2 * dp[i][j]
            dp[i + 1][j] %= MOD
            if j + a <= S:  # 選択肢 1
                dp[i + 1][j + a] += dp[i][j]
                dp[i + 1][j + a] %= MOD
    return dp[N][S]

print(f_knapsack_for_all_subsets())

Fixed submission:
def f_knapsack_for_all_subsets(MOD=998244353):
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    # dp[i][j]: i 番目の要素を見たとき、和が j であるような場合の数
    dp = [[0 for j in range(S + 1)] for i in range(N + 1)]
    dp[0][0] = 1  # インデックスの集合が空のとき、和は 0 の 1 通り
    for i, a in enumerate(A):
        for j in range(S + 1):
            # editorial でいうところの選択肢 2, 3 に相当するので、2 倍
            dp[i + 1][j] += 2 * dp[i][j]
            dp[i + 1][j] %= MOD
            if j + a <= S:  # 選択肢 1
                dp[i + 1][j + a] += dp[i][j]
                dp[i + 1][j + a] %= MOD
    return dp[N][S]

print(f_knapsack_for_all_subsets())
Buggy python submission (verdict: Time Limit Exceeded):
def e_colorful_blocks(MOD=998244353):
    N, M, K = [int(i) for i in input().split()]
    # dp[i][k]: i 番目まで見たときに、同じ色で塗られている組が k 組である
    dp = [[0] * (K + 1) for _ in range(N)]
    dp[0][0] = M

    for i in range(N - 1):
        for k in range(min(i, K) + 1):
            if k == K:
                dp[i + 1][k] += dp[i][k] * (M - 1)  # 異なる色で塗るしかない
                dp[i + 1][k] %= MOD
            else:
                dp[i + 1][k] += dp[i][k] * (M - 1)  # 左と異なる色で塗る
                dp[i + 1][k + 1] += dp[i][k]  # 左と同じ色で塗る
                dp[i + 1][k] %= MOD
                dp[i + 1][k + 1] %= MOD
    return sum([dp[N - 1][k] for k in range(K + 1)]) % MOD

print(e_colorful_blocks())

Fixed submission:
def e_colorful_blocks(MOD=998244353):
    N, M, K = [int(i) for i in input().split()]

    class Combination(object):
        """素数 mod に対する二項係数の計算"""
        __slots__ = ['mod', 'fact', 'factinv']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            inv_append(pow(fac[-1], -1, mod))
            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.fact, self.factinv = mod, fac, inv[::-1]

        def combination(self, n, r):
            return (0 if n < 0 or r < 0 or n < r
                    else self.fact[n] * self.factinv[r] * self.factinv[n - r] % self.mod)

    ans = 0
    comb = Combination(max(N, M), MOD).combination
    for x in range(K + 1):
        a = (M * pow(M - 1, N - 1 - x, MOD)) % MOD
        b = comb(N - 1, x)
        ans = (ans + a * b) % MOD
    return ans

print(e_colorful_blocks())
Buggy python submission (verdict: Runtime Error):
N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
total = sum(A)
print('Yes' if len(1 for a in A if a * 4 * M >= total) >= M else 'No')

Fixed submission:
N, M = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort(reverse=True)
total = sum(A)
ans = 0
for a in A:
    if a * 4 * M >= total:
        ans += 1
print('Yes' if ans >= M else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
def f_knapsack_for_all_segments(MOD=998244353):
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    dp = [[[0] * 3 for s in range(S + 1)] for i in range(N + 1)]
    dp[0][0][0] = 1
    for i in range(N):
        for s in range(S + 1):
            # i 番の要素を採用しない場合
            dp[i + 1][s][0] += dp[i][s][0]
            dp[i + 1][s][1] += dp[i][s][0] + dp[i][s][1]
            dp[i + 1][s][2] += dp[i][s][0] + dp[i][s][1] + dp[i][s][2]
            for t in range(3):
                dp[i + 1][s][t] %= MOD

            # i 番の要素を採用できる場合
            if s + A[i] <= S:
                dp[i + 1][s + A[i]][1] += dp[i][s][0] + dp[i][s][1]
                dp[i + 1][s + A[i]][2] += dp[i][s][0] + dp[i][s][1]
                for t in range(1, 3):
                    dp[i + 1][s + A[i]][t] %= MOD
    return dp[N][S][2]

print(f_knapsack_for_all_segments())


Fixed submission:
def f_knapsack_for_all_segments(MOD=998244353):
    # 参考: https://maspypy.com/atcoder-参加感想-2019-03-22abc-159
    import numpy as np
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    ans = 0
    f = np.zeros(S + 1, np.int64)  # f[k]: 多項式の x**k の項の係数
    for a in A:
        # f で表される多項式に対して (f + 1)(1 + x**a) を新しい f とする
        f[0] += 1  # f + 1
        f[a:] += f[:-a].copy()  # 1 + x**a を掛ける
        f %= MOD
        # x**S の項の係数を解に加える
        ans += f[S]
        ans %= MOD
    return ans

print(f_knapsack_for_all_segments())

Buggy python submission (verdict: Time Limit Exceeded):
def f_knapsack_for_all_segments(MOD=998244353):
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    dp = [[[0] * 3 for s in range(S + 1)] for i in range(N + 1)]
    dp[0][0][0] = 1
    for i in range(N):
        for s in range(S + 1):
            # i 番の要素を採用しない場合
            dp[i + 1][s][0] += dp[i][s][0]
            dp[i + 1][s][1] += dp[i][s][0] + dp[i][s][1]
            dp[i + 1][s][2] += dp[i][s][0] + dp[i][s][1] + dp[i][s][2]
            for t in range(3):
                dp[i + 1][s][t] %= MOD

            # i 番の要素を採用できる場合
            if s + A[i] <= S:
                dp[i + 1][s + A[i]][1] += dp[i][s][0] + dp[i][s][1]
                dp[i + 1][s + A[i]][2] += dp[i][s][0] + dp[i][s][1]
                for t in range(1, 3):
                    dp[i + 1][s + A[i]][t] %= MOD
    return dp[N][S][2]

print(f_knapsack_for_all_segments())

Fixed submission:
def f_knapsack_for_all_segments(MOD=998244353):
    # 参考: https://maspypy.com/atcoder-参加感想-2019-03-22abc-159
    import numpy as np
    N, S = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    ans = 0
    f = np.zeros(S + 1, np.int64)  # f[k]: 多項式の x**k の項の係数
    for a in A:
        # f で表される多項式に対して (f + 1)(1 + x**a) を新しい f とする
        f[0] += 1  # f + 1
        f[a:] += f[:-a].copy()  # 1 + x**a を掛ける
        f %= MOD
        # x**S の項の係数を解に加える
        ans += f[S]
        ans %= MOD
    return ans

print(f_knapsack_for_all_segments())
Buggy python submission (verdict: Runtime Error):
def c_guess_the_number():
    N, M = [int(i) for i in input().split()]
    Conds = [[int(i) for i in input().split()] for j in range(M)]

    digit = [-1] * N
    # 同じ桁に異なる数字を割り当てている場合を検出
    for s, c in Conds:
        if digit[s - 1] not in (-1, c):
            return -1
        digit[s - 1] = c

    # N 桁目に 0 を割り当てなければならない場合
    # 基本的に N+1 桁の数となり矛盾するが、例外的に N = 1 だと 0 とすればよい。
    if digit[0] == 0:
        return -1 if N >= 2 else 0

    # 指定されなかった桁は基本的に 0 とすればよいが、N 桁目は 0 にできない。
    # 例外的に、N = 1, M = 0 だと N 桁目も 0 にできる。
    if N == 1 and M == 0:
        return 0
    for d, value in enumerate(digit):
        if value == -1:
            digit[d] = 0 if d != 0 else 1
    return ''.join(map(str, digit))

print(c_guess_the_number())

Fixed submission:
A = [[int(i) for i in input().split()] for j in range(3)]
N = int(input())
B = [int(input()) for j in range(N)]

for b in B:
    for row in range(3):
        for col in range(3):
            if A[row][col] == b:
                A[row][col] = 0

cond = [
    A[0][0] == A[0][1] and A[0][1] == A[0][2],
    A[1][0] == A[1][1] and A[1][1] == A[1][2],
    A[2][0] == A[2][1] and A[2][1] == A[2][2],
    A[0][0] == A[1][0] and A[1][0] == A[2][0],
    A[0][1] == A[1][1] and A[1][1] == A[2][1],
    A[0][2] == A[1][2] and A[1][2] == A[2][2],
    A[0][0] == A[1][1] and A[1][1] == A[2][2],
    A[0][2] == A[1][1] and A[1][1] == A[2][0]
]
print('Yes' if any(cond) else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
def e_roaming(MOD=10**9 + 7):
    N, K = [int(i) for i in input().split()]
    from operator import mul
    from functools import reduce

    def cmb(n, r):
        if n - r < r:
            r = n - r
        if r == 0:
            return 1
        if r == 1:
            return n

        numerator = [n - r + k + 1 for k in range(r)]
        denominator = [k + 1 for k in range(r)]

        for p in range(2, r + 1):
            pivot = denominator[p - 1]
            if pivot > 1:
                offset = (n - r) % p
                for k in range(p - 1, r, p):
                    numerator[k - offset] /= pivot
                    denominator[k] /= pivot

        result = 1
        for k in range(r):
            if numerator[k] > 1:
                result *= int(numerator[k])
                result %= MOD

        return result

    return cmb(2 * N - 1, N) if K >= N - 1 else sum([cmb(N, k) * cmb(N - 1, k) % MOD for k in range(K + 1)]) % MOD

print(e_roaming())

Fixed submission:
def e_roaming(MOD=10**9 + 7):
    N, K = [int(i) for i in input().split()]

    class Combination(object):
        """参考: https://harigami.net/contents?id=5f169f85-5707-4137-87a5-f0068749d9bb"""
        __slots__ = ['mod', 'factorial', 'inverse']

        def __init__(self, max_val_arg: int = 10**6, mod: int = 10**9 + 7):
            fac, inv = [1], []
            fac_append, inv_append = fac.append, inv.append

            for i in range(1, max_val_arg + 1):
                fac_append(fac[-1] * i % mod)

            # inv_append(pow(fac[-1], -1, mod)) (after Python 3.8)
            inv_append(pow(fac[-1], mod - 2, mod))

            for i in range(max_val_arg, 0, -1):
                inv_append((inv[-1] * i) % mod)

            self.mod, self.factorial, self.inverse = mod, fac, inv[::-1]

        def combination(self, n, r):
            r = min(r, n - r)
            if n < 0 or r < 0 or n < r:
                return 0
            return self.factorial[n] * self.inverse[r] * self.inverse[n - r] % self.mod

    comb = Combination(N).combination
    if K >= N:
        K = N - 1  # N 回以上移動することは、N-1 回移動することと同じ結果を生む
    return sum(comb(N, k) * comb(N - 1, k) % MOD for k in range(K + 1)) % MOD

print(e_roaming())
Buggy python submission (verdict: Time Limit Exceeded):
def f_modularness():
    K, Q = [int(i) for i in input().split()]
    D = [int(i) for i in input().split()]
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    def divceil(a, b):
        return (a + b - 1) // b

    ans = []
    for n, x, m in Queries:
        last, eq = x, 0  # 末項、D_i % m == 0 となる D_i の数
        for i,d in enumerate(D):
            num = divceil(n - 1 - i, K)  # D_i を足すことは何回起きるか？
            last += (d % m) * num
            if d % m == 0:
                eq += num
        ans.append((n - 1) - (last // m - x // m) - eq)
    return '\n'.join(map(str, ans))

print(f_modularness())

Fixed submission:
def f_modularness():
    K, Q = [int(i) for i in input().split()]
    D = [int(i) for i in input().split()]
    Queries = [[int(i) for i in input().split()] for j in range(Q)]

    def divceil(a, b):
        return (a + b - 1) // b

    ans = []
    for n, x, m in Queries:
        last, eq = x, 0  # 末項、D_i % m == 0 となる D_i の数
        for i,d in enumerate(D):
            num = divceil(n - 1 - i, K)  # D_i を足すことは何回起きるか？
            last += (d % m) * num
            if d % m == 0:
                eq += num
        ans.append((n - 1) - (last // m - x // m) - eq)
    return '\n'.join(map(str, ans))

print(f_modularness())
Buggy python submission (verdict: Runtime Error):
def d_dice_in_line():
    from itertools import accumulate
    N, K = [int(i) for i in input().split()]
    P = [int(i) for i in input().split()]

    expected_value = [(p + 1) / 2 for p in P]  # 1~p まで出るサイコロの出目の期待値
    cumsum = [0] + list(accumulate(expected_value))
    return max([cumsum[j + K] - cumsum[j] for j in range(K)])

print(d_dice_in_line())

Fixed submission:
def d_dice_in_line():
    N, K = [int(i) for i in input().split()]
    P = [int(i) for i in input().split()]

    def experience(x):
        return (x + 1) / 2  # 1からxまで出るサイコロの出目の期待値

    ans = []
    tmp = 0
    for i in range(K):
        tmp += experience(P[i])
    ans.append(tmp)
    for i in range(K, N):
        tmp -= experience(P[i - K])
        tmp += experience(P[i])
        ans.append(tmp)
    return max(ans)

print(d_dice_in_line())
Buggy python submission (verdict: Time Limit Exceeded):
def e_crested_ibis_vs_monster_in_place():
    H, N = [int(i) for i in input().split()]
    Monsters = [[int(i) for i in input().split()] for j in range(N)]

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    # としたとき、更新順序を工夫することで添字 i をなくせる
    dp = [0] + [float('inf')] * H
    for a, b in Monsters:
        for j in range(H + 1):
            nj = min(j + a, H)
            dp[nj] = min(dp[nj], dp[j] + b)
    return dp[H]

print(e_crested_ibis_vs_monster_in_place())

Fixed submission:
def e_crested_ibis_vs_monster():
    H, N = [int(i) for i in input().split()]
    Damage, Cost = [], []
    for _ in range(N):
        a, b = [int(i) for i in input().split()]
        Damage.append(a)
        Cost.append(b)

    # dp[i][j]: 最初の i 種類の魔法でモンスターに j ダメージ与える場合の消費魔力の最小値
    dp = [[float('inf')] * (H + 1) for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(N):
        for j in range(H + 1):
            # i 番目の魔法を使わない場合
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
            # i 番目の魔法を使う場合
            dp[i + 1][min(j + Damage[i], H)] = min(dp[i + 1][min(j + Damage[i], H)],
                                                   dp[i + 1][j] + Cost[i])
    return dp[N][H]

print(e_crested_ibis_vs_monster())