Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n, q = map(int, input().split())
s = input()
questions = []
for i in range(q):
    l, r = map(int, input().split())
    questions.append([l - 1, r])

result = [0 for _ in range(q)]
for i in range(1, len(s)):
    if 'A' == s[i - 1] and 'C' == s[i]:
        for index, question in enumerate(questions):
            if question[0] <= i - 1 and question[1] > i:
                result[index] += 1
for num in result:
    print(num)


Fixed submission:
n, q = map(int, input().split())
s = input()
t = [0 for _ in range(n+1)]
for i in range(n):
    t[i+1] = t[i] + (1 if s[i:i+2] == "AC" else 0)

questions = []
for i in range(q):
    l, r = map(int, input().split())
    questions.append([l, r])
for qestion in questions:
    print(t[qestion[1] - 1] - t[qestion[0] - 1])
Buggy python submission (verdict: Time Limit Exceeded):
s = input()

prev = []
for i in range(len(s)):
    prev.append(s[i])
while True:
    result = []
    i = 0
    while i < (len(prev)-1):
        if prev[i] != prev[i+1]:
            i += 2
        else:
            result.append(prev[i])
            i += 1
    if i == (len(prev) - 1):
        result.append(prev[-1])
    if prev == result:
        break
    prev = result
print(len(s) - len(result))


Fixed submission:
s = input()
count = 0
for i in s:
    count += 1 if '1' == i else -1
print(len(s) - abs(count))
Buggy python submission (verdict: Runtime Error):
ｖn, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
inf = 10 ** 18


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


print(dfs(0, 0, 0, 0))


Fixed submission:
"""
できなかった

- 操作 = 四則演算 or 関数
- 交換法則などが成り立つかどうか確認する必要がある
- 再帰的(dfs)に実装する場合、何を初期値として終了条件とするかの理解が必要
  - 前提として全探索であること => 1の解に収束する必要がない
    全ての葉を訪れたときに最小・最大値・解への到達が判定できれば良い

- dfsの実装: いまどの高さにいるかを引数として渡す = 何を高さとして定義するか
  - 階層のインデックスは選択可能な配列のインデックス
  - 同階層のノードは選択肢
  - ノードが持つ値は、その選択をした時の合計の値
  - 終了条件A, B, Cを満たす

- 求める長さA, B, Cの竹 -> 選択肢

"""
(N, A, B, C) = map(int, input().split())
l = [int(input()) for _ in range(N)]


def dfs(index, a, b, c):
    if N == index:
        if 0 < min(a, b, c):
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return 10 ** 9
    costs = []
    costs.append(dfs(index + 1, a, b, c))
    costs.append(dfs(index + 1, a + l[index], b, c) + 10)
    costs.append(dfs(index + 1, a, b + l[index], c) + 10)
    costs.append(dfs(index + 1, a, b, c + l[index]) + 10)
    return min(costs)


print(dfs(0, 0, 0, 0))
Buggy python submission (verdict: Time Limit Exceeded):
(a, b, q) = map(int, input().split())
s = [int(input()) for _ in range(a)]
t = [int(input()) for _ in range(b)]
x = [int(input()) for _ in range(q)]


def find_left_and_right(st, pos):
    for i, s_i in enumerate(st):
        if s_i - pos > 0:
            return (i-1, i)
    return (len(st) - 1, -1)


def find_optimum(ss, ts, pos):
    (sl_index, sr_index) = find_left_and_right(ss, pos)
    (tl_index, tr_index) = find_left_and_right(ts, pos)

    penalty = 9999999999999
    s_left = s[sl_index] if 0 <= sl_index else penalty
    s_right = s[sr_index] if 0 <= sr_index else penalty
    t_left = t[tl_index] if 0 <= tl_index else penalty
    t_right = t[tr_index] if 0 <= tr_index else penalty

    dist0 = abs(s_left - pos) + min(abs(t_left - s_left), abs(t_right - s_left))
    dist1 = abs(s_right - pos) + min(abs(t_left - s_right), abs(t_right - s_right))
    dist2 = abs(t_left - pos) + min(abs(s_left - t_left), abs(s_right - t_left))
    dist3 = abs(t_right - pos) + min(abs(s_left - t_right), abs(s_right - t_right))
    return min([dist0, dist1, dist2, dist3])


for x_i in x:
    print(find_optimum(s, t, x_i))

Fixed submission:
import bisect

(a, b, q) = map(int, input().split())
s = [-10 ** 19]
for _ in range(a):
    s.append(int(input()))
s.append(10 ** 19)
t = [-10 ** 19]
for _ in range(b):
    t.append(int(input()))
t.append(10 ** 19)
x = [int(input()) for _ in range(q)]

for x_i in x:
    ss = bisect.bisect_left(s, x_i)
    tt = bisect.bisect_left(t, x_i)

    sc = [ss - 1, ss]
    tc = [tt - 1, tt]
    optimum = 10 ** 19
    for s_i in sc:
        for t_i in tc:
            dist = abs(s[s_i] - x_i) + abs(s[s_i] - t[t_i])
            if dist < optimum:
                optimum = dist
            dist = abs(t[t_i] - x_i) + abs(t[t_i] - s[s_i])
            if dist < optimum:
                optimum = dist
    print(optimum)
Buggy python submission (verdict: Runtime Error):
(n, m) = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))

score = [0 for _ in range(m)]
for i in a:
    for food_index in range(1, len(i)):
        if 0 >= food_index:
            continue
        score[i[food_index - 1]] += 1
print(score.count(n))

Fixed submission:
(n, m) = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
    a[-1].pop(0)

score = [0 for _ in range(m)]
for i in a:
    for food_index in i:
        score[food_index - 1] += 1
print(score.count(n))

Buggy python submission (verdict: Runtime Error):
(n, m) = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))

score = [0 for _ in range(m)]
for i in a:
    for food_index in range(1, len(i)):
        score[i[food_index - 1]] += 1
print(score.count(n))

Fixed submission:
(n, m) = map(int, input().split())
a = []
for _ in range(n):
    a.append(list(map(int, input().split())))
    a[-1].pop(0)

score = [0 for _ in range(m)]
for i in a:
    for food_index in i:
        score[food_index - 1] += 1
print(score.count(n))
Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
dp = [10 ** 18 for _ in range(n)]
dp = [0]
for i in range(1, n):
    dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]),
                dp[i-2] + abs(h[i] - h[i-2]))
print(dp[-1])


Fixed submission:
n = int(input())
h = list(map(int, input().split()))
dp = [float('INF') for _ in range(n)]
for i in range(n):
    if 0 == i:
        dp[i] = 0
    elif 1 == i:
        dp[i] = abs(h[i-1] - h[i])
    else:
        dp[i] = min(dp[i-1] + abs(h[i-1] - h[i]),
                    dp[i-2] + abs(h[i-2] - h[i]))
print(dp[-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
dp = [0 for _ in range(n)]

def f(cur, prev1, prev2, dp):
    if len(dp) == cur:
        return
    prev1_cost = 100000000 if prev1 < 0 else dp[prev1] + abs(h[cur] - h[prev1])
    prev2_cost = 100000000 if prev2 < 0 else dp[prev2] + abs(h[cur] - h[prev2])
    dp[cur] = prev1_cost if prev1_cost < prev2_cost else prev2_cost
    f(cur + 1, prev1 + 1, prev2 + 1, dp)

f(1, 0, -1, dp)
print(dp[-1])

Fixed submission:
n = int(input())
h = list(map(int, input().split()))
dp = [float('INF') for _ in range(n)]
for i in range(n):
    if 0 == i:
        dp[i] = 0
    elif 1 == i:
        dp[i] = abs(h[i-1] - h[i])
    else:
        dp[i] = min(dp[i-1] + abs(h[i-1] - h[i]),
                    dp[i-2] + abs(h[i-2] - h[i]))
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10 ** 6)

n = int(input())
abc = [list(map(int, input().split())) for _ in range(n)]


def recur(i, last_index, v):
    if i == n:
        return v
    res = -1
    for j in range(3):
        if j == last_index:
            continue
        res = max(res, recur(i + 1, j, v + abc[i][j]))
    return res


print(recur(0, -1, 0))


Fixed submission:
n = int(input())
days = [list(map(int, input().split())) for _ in range(n)]
dp = [[0, 0, 0] for _ in range(n)]

for i in range(n):
    if 0 == i:
        dp[i][0] = days[0][0]
        dp[i][1] = days[0][1]
        dp[i][2] = days[0][2]
    else:
        dp[i][0] = max(days[i][0] + dp[i-1][1], days[i][0] + dp[i-1][2])
        dp[i][1] = max(days[i][1] + dp[i-1][2], days[i][1] + dp[i-1][0],)
        dp[i][2] = max(days[i][2] + dp[i-1][0], days[i][2] + dp[i-1][1],)

print(max(dp[-1]))
Buggy python submission (verdict: Time Limit Exceeded):
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]

for i in range(n):
    for sum_w in range(w + 1):
        if sum_w - wv[i][0] >= 0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i]
                                 [sum_w - wv[i][0]] + wv[i][1])
        dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[n][w])


Fixed submission:
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(w + 1)] for _ in range(n + 1)]

for i in range(n):
    for sum_w in range(w + 1):
        if sum_w - wv[i][0] >= 0:
            dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i]
                                 [sum_w - wv[i][0]] + wv[i][1])
        dp[i+1][sum_w] = max(dp[i+1][sum_w], dp[i][sum_w])
print(dp[n][w])
Buggy python submission (verdict: Runtime Error):
n, w = map(int, input().split())
wv = [list(map(int, input().split())) for _ in range(n)]
v_max = 10 ** 3
w_max = 10 ** 18
dp = [[w_max for _ in range(v_max + 1)] for _ in range(n + 1)]
dp[0][0] = 0

for i in range(1, n + 1):
    for j in range(v_max + 1):
        if 0 == j:
            dp[i][j] = 0
        else:
            w_ = wv[i-1][0]
            v_ = j - wv[i-1][1]
            if v_ >= 0:
                dp[i][j] = min(dp[i-1][v_] + w_, dp[i-1][j])
            else:
                dp[i][j] = dp[i-1][j]


for i in range(len(dp[-1]), -1, -1):
    if dp[-1][i] <= w:
        print(i)
        exit(0)


Fixed submission:
#!/usr/bin/env python3
import sys


def solve(N: int, W: int, w: "List[int]", v: "List[int]"):
    INF = 10**10
    VMAX=10**3*N
    dp=[[INF for j in range(VMAX+1)] for i in range(N+1)]
    dp[0][0] = 0
    for i in range(1,N+1):
        for j in range(VMAX+1):
            if j-v[i-1] >=0:
                dp[i][j] = min(dp[i-1][j],dp[i-1][j-v[i-1]]+w[i-1])
            else:
                dp[i][j] = dp[i-1][j]

    for i in range(VMAX,-1,-1):
        if dp[-1][i] <= W:
            print(i)
            break
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    w = [int()] * (N)  # type: "List[int]"
    v = [int()] * (N)  # type: "List[int]"
    for i in range(N):
        w[i] = int(next(tokens))
        v[i] = int(next(tokens))
    solve(N, W, w, v)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
p = []
for i in range(int(input())):
    p.append(int(input()))

p.sort().reverse()
total = int(p[0] * 0.5)
for i in range(1, len(p)):
    total += p[i]
print(total)

Fixed submission:
n = int(input())
p = [int(input()) for _ in range(n)]
p.sort(reverse=True)
sum = int(p[0] / 2)
for i in range(1, len(p)):
	sum += p[i]
print(sum)
Buggy python submission (verdict: Time Limit Exceeded):
"""
検討
- 閉路が存在する場合、スコアをいくらでも大きくできる可能性がある
- 閉路を1周することで得られるスコア合計の符合によってinfにできるかどうかが判定可能
  - 符合が正: inf
  - 符合が0以下: 最短でNに到達したときのスコアが解
- 閉路が複数ある場合はどうするか
  - 1周したときに得られるスコア合計の符合が正かどうかで判断

必要な処理
- 最短経路探索: ダイクストラ法? -> ベルマンフォード法（コストに正負があるため）
- 閉路を見つけ出す
- 閉路を1周したときのスコア合計
"""

n, m = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    edges.append([a-1, b-1, -c])

inf = 10 ** 18
dist = [inf for _ in range(n)]
dist[0] = 0

for i in range(n-1):
    for edge in edges:
        if dist[edge[0]] == inf:
            continue
        if dist[edge[1]] > dist[edge[0]] + edge[2]:
            dist[edge[1]] = dist[edge[0]] + edge[2]
ans = -dist[n-1]

negative = [False for _ in range(n)]
for i in range(n):
    for edge in edges:
        if dist[edge[0]] == inf:
            continue
        if dist[edge[1]] > dist[edge[0]] + edge[2]:
            dist[edge[1]] = dist[edge[0]] + edge[2]
            negative[edge[1]] = True
        if negative[edge[0]]:
            negative[edge[1]] = True

if negative[n-1]:
    print("inf")
else:
    print(ans)


Fixed submission:
"""

問題:
N 頂点 M 辺の重み付き有向グラフがあります。
i(1≦i≦M) 番目の辺は 頂点 ai から 頂点 bi を重み ciで結びます。
このグラフと駒を利用して、次の1人ゲームを行います。
最初、駒を頂点 1に置いて、プレイヤーのスコアを 0とします。
プレイヤーは、次の条件で駒を繰り返し移動させることができます。
- 頂点 aiに駒があるとき、i 番目の辺を利用して頂点 bi に移動する。移動後にプレイヤーのスコアが ci加算される。
頂点 Nに駒があるときのみ、ゲームを終了できます。
なお、与えられる有向グラフの上で頂点 1 から頂点 Nに移動できることは保障されています。
プレイヤーがゲーム終了時のスコアを出来るだけ大きくするような行動を取ったとき、ゲーム終了時のスコアはいくつになるでしょうか?
ゲーム終了時のスコアをいくらでも大きくできる場合は inf と出力してください。

制約:
- 2≦N≦1000
- 1≦M≦min(N(N−1),2000)
- 1≦ai,bi≦N(1≦i≦M)
- ai≠bi(1≦i≦M)
- ai≠ajまたは bi≠bj(1≦i<j≦M)
- −10 ** 9≦ci≦10 ** 9(1≦i≦M)
- ciは整数である。
- 与えられるグラフには、頂点 1から頂点 N への経路が存在する。

検討:
- 閉路が存在する場合、スコアをいくらでも大きくできる可能性がある
- 閉路を1周することで得られるスコア合計の符合によってinfにできるかどうかが判定可能
  - 符合が正: inf
  - 符合が0以下: 最短でNに到達したときのスコアが解
- 閉路が複数ある場合はどうするか
  - 1周したときに得られるスコア合計の符合が正かどうかで判断

必要な処理:
- 最短経路探索: ダイクストラ法? -> ベルマンフォード法（コストに正負があるため）
- 閉路を見つけ出す
- 閉路を1周したときのスコア合計

計算時間:
- ベルマンフォード法の時間計算量はO(NM)
- 解説通りにpythonで実装したらTLE判定...(約2100ms)
- 制約通りでいけば、NM = 1000 * 2000 = 2 * 10 ** 6
- 20msの化け物がいる...
"""

n, m = map(int, input().split())
edges = []
for _ in range(m):
    a, b, c = map(int, input().split())
    edges.append([a-1, b-1, -c])

inf = 10 ** 18
dist = [inf for _ in range(n)]
dist[0] = 0

for i in range(n-1):
    for a, b, c in edges:
        dist[b] = min(dist[b], dist[a] + c)

for i in range(n):
    for a, b, c in edges:
        if dist[a] + c < dist[b]:
            dist[b] = -inf

if -inf == dist[-1]:
    print("inf")
else:
    print(-dist[-1])
Buggy python submission (verdict: Runtime Error):
s = input()
result = []
for i in range(len(s)):
  if '0' == i[s]:
    result.append(0)
  elif '1' == i[s]:
    result.append(1)
  elif 'B' == i[s]:
    if 0 == len(result):
      pass
    else:
      result.pop()
print(result)

Fixed submission:
s = input()
result = []
for i in range(len(s)):
  if 'B' == s[i]:
    if 0 == len(result):
      pass
    else:
      result.pop()
  else:
    result.append(s[i])    
print("".join(result))
Buggy python submission (verdict: Time Limit Exceeded):
# n, lは最大100
# 計算量はn^2
# これであれば全探索いけるか
import itertools

(n, l) = map(int, input().split())
s = [input() for _ in range(n)]

candidates = []
index_candidates = list(itertools.permutations(range(len(s))))
for c in index_candidates:
  str = ""
  for index in c:
    str += s[index]
  candidates.append(str)

min_str = candidates[0]
for i in range(1, len(candidates)):
  for j in range(len(min_str)):
    if min_str[j] > candidates[i][j]:
      min_str = candidates[i]
      break
    elif min_str[j] < candidates[i][j]:
      break
print(min_str)

Fixed submission:
(n, l) = map(int, input().split())
s = "".join(sorted([input() for _ in range(n)]))
print(s)
Buggy python submission (verdict: Runtime Error):
n=int(input())
ei="$abcdefghijklmnopqrstuvwxyz"
fla=[]
flaa=[0]
ans=""
cnt=2

for i in range(1,11):
    fla.append(26**i)
    flaa.append(flaa[i-1]+26**i)
for i in range(10):
    if n<=flaa[i]:
        k=i
        break
# print(flaa,k)
if k==1:
    print(ei[n])
    exit()

b=n-flaa[k-1]
# print(b)
while len(ans)!=k-1:
    for i in range(1,27):
        if fla[k-cnt]*i>=b:
            ans+=str(ei[i])
            b-=fla[k-cnt]*(i-1)
            cnt+=1
            # print(b)
            break
ans+=str(ei[b])

print(ans)


Fixed submission:
n=int(input())
ei="$abcdefghijklmnopqrstuvwxyz"
fla=[]
flaa=[0]
ans=""
cnt=2

for i in range(1,20):
    fla.append(26**i)
    flaa.append(flaa[i-1]+26**i)
for i in range(20):
    if n<=flaa[i]:
        k=i
        break
# print(flaa,k)
if k==1:
    print(ei[n])
    exit()

b=n-flaa[k-1]
# print(b)
while len(ans)!=k-1:
    for i in range(1,27):
        if fla[k-cnt]*i>=b:
            ans+=str(ei[i])
            b-=fla[k-cnt]*(i-1)
            cnt+=1
            # print(b)
            break
ans+=str(ei[b])

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
k=int(input())
s=input()
ans=0
mod=1000000007
x=2200000
fact=[0]*x
invfact=[0]*x
fact[0]=1
for i in range(1,x):
    fact[i]=fact[i-1]*i%mod
invfact[x-1]=pow(fact[x-1],mod-2,mod)
for i in range(x-2,-1,-1):
    invfact[i]=invfact[i+1]*(i+1)%mod
def nCk(n,k):
    return(fact[n]*invfact[k]*invfact[n-k])%mod
for i in range(0,k+1):
    # print(nCk(len(s)+i-1,i),pow(25,i,mod),pow(26,k-i,mod))
    ans+=nCk(len(s)+i-1,i)*pow(25,i,mod)*pow(26,k-i,mod)
print(ans%mod)

Fixed submission:
k=int(input())
s=input()
ans=0
mod=1000000007
x=2200000
fact=[0]*x
invfact=[0]*x
fact[0]=1
for i in range(1,x):
    fact[i]=fact[i-1]*i%mod
invfact[x-1]=pow(fact[x-1],mod-2,mod)
for i in range(x-2,-1,-1):
    invfact[i]=invfact[i+1]*(i+1)%mod
def nCk(n,k):
    return(fact[n]*invfact[k]*invfact[n-k])%mod
for i in range(0,k+1):
    # print(nCk(len(s)+i-1,i),pow(25,i,mod),pow(26,k-i,mod))
    ans+=nCk(len(s)+i-1,i)*pow(25,i,mod)*pow(26,k-i,mod)
print(ans%mod)

Buggy python submission (verdict: Runtime Error):
k=int(input())
s=input()
ans=0
mod=1000000007
x=220000
fact=[0]*x
invfact=[0]*x
fact[0]=1
for i in range(1,x):
    fact[i]=fact[i-1]*i%mod
invfact[x-1]=pow(fact[x-1],mod-2,mod)
for i in range(x-2,-1,-1):
    invfact[i]=invfact[i+1]*(i+1)%mod
def nCk(n,k):
    return(fact[n]*invfact[k]*invfact[n-k])%mod
for i in range(0,k+1):
    # print(nCk(len(s)+i-1,i),pow(25,i,mod),pow(26,k-i,mod))
    ans+=nCk(len(s)+i-1,i)*pow(25,i,mod)*pow(26,k-i,mod)
print(ans%mod)

Fixed submission:
k=int(input())
s=input()
ans=0
mod=1000000007
x=2200000
fact=[0]*x
invfact=[0]*x
fact[0]=1
for i in range(1,x):
    fact[i]=fact[i-1]*i%mod
invfact[x-1]=pow(fact[x-1],mod-2,mod)
for i in range(x-2,-1,-1):
    invfact[i]=invfact[i+1]*(i+1)%mod
def nCk(n,k):
    return(fact[n]*invfact[k]*invfact[n-k])%mod
for i in range(0,k+1):
    # print(nCk(len(s)+i-1,i),pow(25,i,mod),pow(26,k-i,mod))
    ans+=nCk(len(s)+i-1,i)*pow(25,i,mod)*pow(26,k-i,mod)
print(ans%mod)
Buggy python submission (verdict: Time Limit Exceeded):

a=int(input())
b=list(map(int, input().split()))
c=1
d=0
if 0 in b:
    d=1
for i in range(a):
    c*=b[i]
    if d!=1 and c>10**18:
        print(-1)
        exit()


print(c)


Fixed submission:

a=int(input())
b=list(map(int, input().split()))
c=1
d=0
if 0 in b:
    print(0)
    exit()
for i in range(a):
    c*=b[i]
    if c>10**18:
        print(-1)
        exit()


print(c)

Buggy python submission (verdict: Runtime Error):

a=int(input())
b=list(map(int, input().split()))
c=1
if 0 in b:
    d=1
for i in range(a):
    c*=b[i]
    if d!=1 and c>10**18:
        print(-1)
        exit()


print(c)


Fixed submission:

a=int(input())
b=list(map(int, input().split()))
c=1
d=0
if 0 in b:
    print(0)
    exit()
for i in range(a):
    c*=b[i]
    if c>10**18:
        print(-1)
        exit()


print(c)
Buggy python submission (verdict: Runtime Error):

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if c>k:
    if c-1>=k:

        print(b[k])
        exit()
if 1 not in b:
    # print(b,f)
    ans=(k-e)%f
    print(b[ans])
else:

    ans=k%f
    print(b[ans])

Fixed submission:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
bc=b
b=b[e:c+1]
f=c+1-e
if c>k:
    if c-1>=k:

        print(bc[k])
        exit()
if 1 not in b:
    # print(b,f)
    ans=(k-e)%f
    print(b[ans])
else:

    ans=k%f
    print(b[ans])

Buggy python submission (verdict: Runtime Error):

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
b=b[e:c+1]
f=c+1-e
if n>k:
    print(b[k])
    exit()
if 1 not in b:
    ans=(k-e)%f
    print(b[ans])
else:
    # print(b,f)
    ans=k%f
    print(b[ans])


Fixed submission:

n,k=map(int,input().split())
a=list(map(int, input().split()))
b=[1]
ba={1}
c=0
m=0
# while flag==1:
for i in range(n):
    if a[m] not in ba:
        b.append(a[m])
        ba.add(a[m])
        c+=1
        m=a[m]-1
    else:
        e=a[m]
        break
# print(b,e,c+1)
e=b.index(e)
# print(b,e)
bc=b
b=b[e:c+1]
f=c+1-e
if c>k:
    if c-1>=k:

        print(bc[k])
        exit()
if 1 not in b:
    # print(b,f)
    ans=(k-e)%f
    print(b[ans])
else:

    ans=k%f
    print(b[ans])
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int, input().split()))
ans=0
for i in range(n-1):
    for j in range(i,n):
        if j-i == a[j]+a[i]:
            ans+=1


print(ans)


Fixed submission:
import collections
n=int(input())
a=list(map(int, input().split()))
ans=0
b=[]
c=[]
for i in range(n):
    b.append(i+a[i])
    c.append(i-a[i])
b=collections.Counter(b)
c=collections.Counter(c)
for i in range((10**5)*2):
    ans+=b[i]*c[i]



print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math
mf=math.floor
a,b,n=map(int,input().split())
d=0
for i in range(n+1):
    c=mf(a*i/b)-a*mf(i/b)
    d=max(d,c)
print(d)


Fixed submission:
import math
mf=math.floor
a,b,n=map(int,input().split())
d=0
if n<b:
    i=n
elif n>=b:
    i=b-1
c=mf(a*i/b)-a*mf(i/b)
print(c)
Buggy python submission (verdict: Time Limit Exceeded):

import collections
n=int(input())
s=list(map(int, input().split()))
c=collections.Counter(s)
for i in range(1,n+1):
    b=s.count(i)
    print(b)


Fixed submission:

import collections
n=int(input())
s=list(map(int, input().split()))
c=collections.Counter(s)
for i in range(1,n+1):

    print(c[i])
Buggy python submission (verdict: Time Limit Exceeded):
a,b,m=map(int,input().split())
ai=list(map(int, input().split()))
bi=list(map(int, input().split()))
sm=[list(map(int, input().split())) for _ in range(m)]
ans=10**6
for i in range(a):
    for j in range(b):
        for x in range(m):
            k=ai[i]+bi[j]
            if sm[x][0]==i+1 and sm[x][1]==j+1:
                k=k-sm[x][2]
            if ans >=k:
                ans=k
print(ans)


Fixed submission:
a,b,m=map(int,input().split())
ai=list(map(int, input().split()))
bi=list(map(int, input().split()))
sm=[list(map(int, input().split())) for _ in range(m)]
k=min(ai)+min(bi)

for x in range(m):
    
    l=ai[sm[x][0]-1]+bi[sm[x][1]-1]-sm[x][2]
    k=min(k,l)

print(k)