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

Fixed submission:
N, K = list(map(int, input().split()))
td = []
for i in range(N):
  td.append(list(map(int, input().split())))

td.sort(key = lambda x:x[1], reverse = True)

D = []
T = {}
d = 0

for i in range(K):
  D.append(td[i])
  d += td[i][1]
  t = td[i][0]
  if t in T:
    T[t] += 1
  else:
    T[t] = 1

D.sort(key = lambda x: x[1])

cnt = 0
flag = False
Ans = d + pow(len(T), 2)
for i in range(K, N):
  if td[i][0] in T:
    continue
  while T[D[cnt][0]] == 1:
    cnt += 1
    if cnt == K:
      flag = True
      break
  if flag:
    break

  T[td[i][0]] = 1
  T[D[cnt][0]] -= 1
  d -= D[cnt][1] - td[i][1]
  
  t = d + pow(len(T), 2)
  if Ans < t:
    Ans = t
  cnt += 1
  if cnt == K:
    break

print(Ans)

Buggy submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
td = []
for i in range(N):
  td.append(list(map(int, input().split())))

td.sort(key = lambda x:x[1], reverse = True)

D = []
T = {}
d = 0

for i in range(K):
  D.append(td[i])
  d += td[i][1]
  t = td[i][0]
  if t in T:
    T[t] += 1
  else:
    T[t] = 1

D.sort(key = lambda x: x[1])

cnt = 0
flag = False
Ans = d + pow(len(T), 2)
for i in range(K, N):
  if td[i][0] in T:
    continue
  while T[D[cnt][0]] == 1:
    cnt += 1
    if cnt == N:
      flag = True
      break
  if flag:
    break

  T[td[i][0]] = 1
  T[D[cnt][0]] -= 1
  d -= D[cnt][1] - td[i][1]
  
  t = d + pow(len(T), 2)
  if Ans < t:
    Ans = t
  cnt += 1
  if cnt == N:
    break


print(Ans)
Problem p03157 (python)

Fixed submission:
def getminLT(i):
  if LT[i] == i:
    return i
  else:
    return getminLT(LT[i])

H, W = list(map(int, input().split()))

D = []
for i in range(H):
  D.append(input())

M = [[0] * W for i in range(H)]
LT = [0]
for i in range(H):
  for j in range(W):
    if i == 0 and j == 0:
      continue
    flag = False
    if i != 0:
      if D[i][j] != D[i-1][j]:
        M[i][j] = M[i-1][j]
        flag = True
    if j != 0:
      if D[i][j] != D[i][j-1]:
        M[i][j] = M[i][j-1]
        if flag == True:
          
          LT[max(getminLT(M[i-1][j]), getminLT(M[i][j-1]))] = LT[min(getminLT(M[i][j-1]), getminLT(M[i-1][j]))]
        flag = True
    if flag == False:
        M[i][j] = len(LT)
        LT.append(len(LT))
for i in range(len(LT)):
  LT[i] = getminLT(LT[i])
  
L = [[] for i in range(len(LT))]
for i in range(H):
  for j in range(W):
  	L[LT[M[i][j]]].append((i, j))
#print(M)
#print(LT)
#print(L)
cnt = 0
for i in L:
  E = 0
  A = 0
  for j in i:
    if (j[0] + j[1]) % 2 == 0:
      E += 1
    else:
      A += 1
  cnt += E * A
print(cnt)

Buggy submission (verdict: Runtime Error):
def check(i, j):
  T = []
  if i >= 1:
    if M[i-1][j] == 0:
      if D[i][j] != D[i-1][j]:
        T.append((i-1, j))
        M[i-1][j] = 1
        T.extend(check(i-1, j))
  if i < H - 1:
    if M[i+1][j] == 0:
      if D[i][j] != D[i+1][j]:
        T.append((i+1, j))
        M[i+1][j] = 1
        T.extend(check(i+1,j))
  if j >= 1:
    if M[i][j-1] == 0:
      if D[i][j] != D[i][j-1]:
        T.append((i, j-1))
        M[i][j-1] = 1
        T.extend(check(i, j-1))
  if j < W - 1:
    if M[i][j+1] == 0:
      if D[i][j] != D[i][j+1]:
        T.append((i, j+1))
        M[i][j+1] = 1
        T.extend(check(i, j+1))
  return T

H, W = list(map(int, input().split()))

D = []
for i in range(H):
  D.append(input())

M = [[0] * W for i in range(H)]
L = []
for i in range(H):
  for j in range(W):
    if M[i][j] == 0:
      L.append(check(i, j))

cnt = 0
for i in L:
  E = 0
  A = 0
  for j in i:
    if (j[0] + j[1]) % 2 == 0:
      E += 1
    else:
      A += 1
  cnt += E * A
print(cnt)
Problem p03177 (python)

Fixed submission:
import numpy as np

N, K = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(N)]
MOD = 10 ** 9 + 7

A = np.array(a, dtype = 'O')
A = A.T

ans = np.eye(N, dtype = 'O')
while K:
  if K & 1:
    ans = np.dot(ans, A)
    ans %= MOD
  A = np.dot(A, A)
  A %= MOD
  K >>= 1

print(np.sum(abs(ans)) % MOD)

Buggy submission (verdict: Runtime Error):
import numpy as np

N, K = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(N)]
MOD = 10 ** 9 + 7

A = np.array(a, dtype = 'int64')
A = A.T

ans = np.eye(N, dtype = 'int64')
while K:
  if K & 1:
    ans = np.dot(ans, A)
    ans %= MOD
  A = np.dot(A, A)
  A %= MOD
  K >>= 1

print(int(np.sum(abs(ans))) % MOD)
Problem p03231 (python)

Fixed submission:
def gcd(x, y):
  n = min(x, y)
  for i in range(n, 0, -1):
    if x % i == 0 and y % i == 0:
      return i

def lcm(x, y):
  return (x * y) // gcd(x,y)


N, M = list(map(int, input().split()))
S = list(input())
T = list(input())

L = lcm(N, M)

x = L // N
y = L // M
for i in range(N):
  if (i * x) % y == 0:
    if S[i] != T[(i * x) // y]:
      L = -1
      break

print(L)

Buggy submission (verdict: Runtime Error):
import math
def prime(x):
  p = {}
  cnt = 2
  Flag = True
  while Flag:
    last = math.ceil(x ** 0.5) 
    if cnt > last:
      break
    for i in range(cnt, last + 1):
      if x % i == 0:
        x //= i
        p[i] = 1
        cnt = i + 2
        while x % i == 0:
          x //= i
          p[i] += 1
        cnt = i + 1
        break
      if i == last:
        Flag = False
  if x != 1:
    p[x] = 1
  return p

def lcm(x, y):
  return (x * y) // math.gcd(x,y)


N, M = list(map(int, input().split()))
S = list(input())
T = list(input())

L = lcm(N, M)

x = L // N
y = L // M
for i in range(N):
  if (i * x) % y == 0:
    if S[i] != T[(i * x) // y]:
      L = -1
      break

print(L)
Problem p03262 (python)

Fixed submission:
####################
def pri(n):
  i = 2
  X = {}
  while i * i <= n:
    while n % i == 0:
      n /= i
      if i in X:
        X[i] = X[i] + 1
      else:
        X[i] = 1
    i += 1
  if n > 1:
    X[n] = 1
  return X
####################


N,X = list(map(int, input().split()))

x = list(map(int, input().split()))
x.append(X)

x.sort()

d = []
for i in range(N):
  dif = x[i+1] - x[i]
  if dif not in d:
    d.append(dif)

d.sort()

if len(d) == 1:
  print(d[0])
  exit()

m = d[0]
P = pri(d[0])

for i in range(1,len(d)):
  if d[i] % m != 0:
    t = pri(d[i])
    m = 1
    for j in list(P):
      if j in t:
        P[j] = min(P[j], t[j])
        m *= j ** P[j]
      else:
        del P[j]

print(int(m))

Buggy submission (verdict: Runtime Error):
####################
def pri(n):
  i = 2
  X = {}
  while i * i <= n:
    while n % i == 0:
      n /= i
      if i in X:
        X[i] = X[i] + 1
      else:
        X[i] = 1
    i += 1
  if n > 1:
    X[n] = 1
  return X
####################


N,X = list(map(int, input().split()))

x = list(map(int, input().split()))
x.append(X)

x.sort()

d = []
for i in range(N):
  dif = x[i+1] - x[i]
  if dif not in d:
    d.append(dif)

d.sort()

if len(d) == 1:
  print(d[0])
  exit()

min = d[0]
P = pri(d[0])

for i in range(1,len(d)):
  if d[i] % min != 0:
    t = pri(d[0])
    min = 1
    for j in P:
      if j in t:
        P[j] = min(P[j], t[j])
        min *= j ** P[j]
      else:
        del P[j]

print(min)
Problem p03282 (python)

Fixed submission:
import re
S = input()
K = int(input())

d = re.search('[2-9]', S)
if d == None or d.start() >= K:
  print("1")
else:
  print(d.group())

Buggy submission (verdict: Runtime Error):
import re
S = input()
K = int(input())

d = re.search('[2-9]', S)
if d.start() >= K:
  print("1")
else:
  print(d.group())
Problem p03328 (python)

Fixed submission:
a, b = list(map(int, input().split()))

c = b - a

east = (1 + c) * c // 2

print(east - b)

Buggy submission (verdict: Runtime Error):
def su(c):
  if c == 1:
    return 1
  else:
    return c + su(c - 1)

a, b = list(map(int, input().split()))

c = b - a

east = su(c)

print(east - b)
Problem p03340 (python)

Fixed submission:
N = int(input())
A = tuple(map(int, input().split()))
if len(A) == 1:
  print(1)
  exit()
r = 1
cnt = 1
T = A[0]
t = 1
flg = False
for l in range(N):
  while True:
    if T ^ A[r] == T + A[r]:
      t += 1
      cnt += t
      T += A[r]
      r += 1
    else:
      T -= A[l]
      t -= 1
      break
    if r == N:
      flg = True
      break
  if flg:
    break
print(cnt)

Buggy submission (verdict: Runtime Error):
N = int(input())
A = tuple(map(int, input().split()))
r = 1
cnt = 1
T = A[0]
t = 1
flg = False
for l in range(N):
  while True:
    if T ^ A[r] == T + A[r]:
      t += 1
      cnt += t
      T += A[r]
      r += 1
    else:
      T -= A[l]
      t -= 1
      break
    if r == N:
      flg = True
      break
  if flg:
    break
print(cnt)

Buggy submission (verdict: Runtime Error):
import numpy as np

def main():
  N = int(input())
  A = np.asarray(list(map(int, input().split())), dtype=np.float32)
  r = 1
  cnt = 1
  T = A[0]
  t = 1
  flg = False
  for l in range(N):
    while True:
      if int(T) ^ int(A[r]) == T + A[r]:
        t += 1
        cnt += t
        T += A[r]
        r += 1
      else:
        T -= A[l]
        t -= 1
        break
      if r == N:
        flg = True
        break
    if flg:
      break
  print(cnt)  

main()

Buggy submission (verdict: Runtime Error):
import numpy as np

def main():
  N = int(input())
  A = np.asarray(list(map(int, input().split())))
  r = 1
  cnt = 1
  T = A[0]
  t = 1
  flg = False
  for l in range(N):
    while True:
      if T ^ A[r] == T + A[r]:
        t += 1
        cnt += t
        T += A[r]
        r += 1
      else:
        T -= A[l]
        t -= 1
        break
      if r == N:
        flg = True
        break
    if flg:
      break
  print(cnt)  

main()

Buggy submission (verdict: Runtime Error):
def main():
  N = int(input())
  A = tuple(map(int, input().split()))
  r = 1
  cnt = 1
  T = A[0]
  t = 1
  flg = False
  for l in range(N):
    while True:
      if T ^ A[r] == T + A[r]:
        t += 1
        cnt += t
        T += A[r]
        r += 1
      else:
        T -= A[l]
        t -= 1
        break
      if r == N:
        flg = True
        break
    if flg:
      break
  print(cnt)  

main()

Buggy submission (verdict: Runtime Error):
def main():
  N = int(input())
  A = list(map(int, input().split()))
  r = 1
  cnt = 1
  T = A[0]
  t = 1
  flg = False
  for l in range(N):
    while True:
      if T ^ A[r] == T + A[r]:
        t += 1
        cnt += t
        T += A[r]
        r += 1
      else:
        T -= A[l]
        t -= 1
        break
      if r == N:
        flg = True
        break
    if flg:
      break
  print(cnt)  

main()
Problem p03354 (python)

Fixed submission:
def Root(x, cnt):
  cnt += 1
  if L[x] == x:
    return x, cnt
  else:
    return Root(L[x], cnt)

N, M = list(map(int, input().split()))
p = list(map(int, input().split()))

Data = []
for i in range(M):
  Data.append(list(map(int, input().split())))

L = [i for i in range(N + 1)]
for i in Data:
  a = Root(i[0], 0)
  b = Root(i[1], 0)
  if a[1] > b[1]:
    L[b[0]] = L[a[0]]
  else:
    L[a[0]] = L[b[0]]
    
Ans = 0
for i in range(N):
  if Root(i + 1, 0)[0] == Root(p[i], 0)[0]:
    Ans += 1
print(Ans)

Buggy submission (verdict: Runtime Error):
def Root(x):
  if L[x] == x:
    return x
  else:
    return Root(L[x])

N, M = list(map(int, input().split()))
p = list(map(int, input().split()))
L = [i for i in range(N + 1)]
for i in range(M):
  t = list(map(int, input().split()))
  L[Root(min(t))] = L[Root(max(t))]

Ans = 0
for i in range(N):
  if Root(i + 1) == Root(p[i]):
    Ans += 1
print(Ans)
Problem p03385 (python)

Fixed submission:
S = input()

if "a" not in S:
  print("No")
  exit()

if "b" not in S:
  print("No")
  exit()

if "c" not in S:
  print("No")
  exit()

print("Yes")

Buggy submission (verdict: Runtime Error):
S = input()

if "a" not in S:
  print("No")
  exit()

if "b" not in S:
  print("No")
  exit()

if "c" not in S:
  print("No")
  exit()

 print("Yes")
Problem p03423 (python)

Fixed submission:
N = int(input())

print(N // 3)

Buggy submission (verdict: Runtime Error):
N = int(ipnut())

print(3 // N)
Problem p03738 (python)

Fixed submission:
A = int(input())
B = int(input())

if A == B:
  print("EQUAL")
elif A > B:
  print("GREATER")
else:
  print("LESS")

Buggy submission (verdict: Runtime Error):
A = int(input())
B = Int(input())

if A == B:
  print("EQUAL")
elif A > B:
  print("GREATER")
else:
  print("LESS")
Problem p03762 (python)

Fixed submission:
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
y = list(map(int, input().split()))
MOD = 10 ** 9 + 7
X = 0
Y = 0
for i in range(n - 1):
  t = x[i + 1] - x[i]
  X = (X + (i + 1) * (n - i - 1) * t) % MOD
for i in range(m - 1):
  t = y[i + 1] - y[i]
  Y = (Y + (i + 1) * (m - i - 1) * t) % MOD
print(X * Y % MOD)

Buggy submission (verdict: Runtime Error):
def exe(x, y):
  X = [0] * x
  Y = [0] * y
  for i in range(x):
    X[i] = (i + 1) * (x - i) * a[i] % MOD
  for i in range(y):
    Y[i] = (i + 1) * (y - i) * b[i] % MOD
  print((sum(X) % MOD) * (sum(Y) % MOD) % MOD)
  

n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
y = list(map(int, input().split()))
MOD = 10 ** 9 + 7
a = [0] * (n - 1)
for i in range(n - 1):
  a[i] = x[i + 1] - x[i]
b = [0] * (n - 1)
for i in range(m - 1):
  b[i] = y[i + 1] - y[i]
exe(n - 1, m - 1)
Problem p03780 (python)

Fixed submission:
def hersCode(l, r):
  ok = r
  ng = l - 1
  while ok - ng > 1:
    mid = (ok + ng) // 2
    if nasu(a[mid]):
      ok = mid
    else:
      ng = mid
  return ok

def nasu(x):
  if x >= K:
    return True
  D = [0] * K
  L = [0]
  f = 0
  for i in a:
    if i == x and f == 0:
      f = 1
      continue
    for jj in range(len(L) - 1, -1, -1):
      j = L[jj]
      if i + j < K:
        if i + j + x >= K:
          return True
        if D[i + j] == 0:
          D[i + j] = 1
          L.append(i + j)
  return False


N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

a.sort()
a.append(10 ** 9 + 1)
ans = hersCode(0, N)
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))

a.sort()
ans = 0

for i in range(N):
  if a[-1 -i] < K:
    break
  ans += 1

n = N - ans
D = []
re = a[0]
cnt = 0
for i in range(n):
 if re == a[i]:
   cnt += 1
 else:
   D.append([re, cnt])
   cnt = 1
 re = a[i]
else:
  D.append([re, cnt])

L = [{} for _ in range(2 * K)]
M = [[0] * (2 * K) for _ in range(2 * K)]
for i in D:
  for j in range(i[1]):
    if i[0] * (j + 1) >= 2 * K:
      break
    L[i[0] * (j + 1)][i[0]] = 1
    M[i[0] * (j + 1)][i[0]] = 1

for i in D:
  for j in range(i[1]):
    for k in range(K):
      t = i[0] * (j + 1)
      if k + t >= 2 * K:
        break
      if len(L[k]) > 0:
        if M[k][t] == 0:
          for l in L[k].keys():
            M[k + t][l] = 1
            L[k + t][l] = 1
          M[k + t][t] = 1
          L[k + t][l] = 1

for i in D:
  for j in range(K, K + i[0]):
    if len(L[j]) > 0 and M[j][i[0]] == 1:
      ans += i[1]
      break

print(N - ans)
Problem p02615 (python)

Fixed submission:
n=input()
a=[int(i) for i in raw_input().split()]
a.sort()
s=a[n-1]
for i in range(n/2,n-1):
  s+=a[i]*2
if(n%2==1):
  s-=a[n/2]
print s

Buggy submission (verdict: Runtime Error):
n=input()
a=[int(i) for i in raw_input().split()]
s=0
a.sort()
for i in range(1,n):
  s+=a[i]
 print s
Problem p02658 (python)

Fixed submission:
import sys
# from collections import defaultdict
# from collections import Counter
# from queue import Queue
import math

def main():
    N = int(input())

    As = [int(s) for s in input().split()]
    logsum = 0
    prod = 1
    for a in As:
        if a == 0:
            print('0')
            return

        logsum += math.log10(a)
        # sys.stderr.write('{}\n'.format(logsum))


    if logsum > 18.2:
        print('-1')
        return

    for a in As:
        prod *= a

    if prod > 10 ** 18:
        print('-1')
        return
    print(prod)

    # As = []
    # for i in range(N):
    #     tmp = int(input())
    #     As.append(tmp)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Buggy submission (verdict: Runtime Error):
import sys
# from collections import defaultdict
# from collections import Counter
# from queue import Queue
import math

def main():
    N = int(input())

    As = [int(s) for s in input().split()]
    logsum = 0
    prod = 1
    for a in As:
        logsum += math.log10(a)

    if logsum >= 18:
        print('-1')
        return

    for a in As:
        prod *= a

    print(prod)

    # As = []
    # for i in range(N):
    #     tmp = int(input())
    #     As.append(tmp)

    return 0

if __name__ == '__main__':
    sys.exit(main())

Buggy submission (verdict: Runtime Error):
import sys
# from collections import defaultdict
# from collections import Counter
# from queue import Queue
import math

def main():
    N = int(input())

    As = [int(s) for s in input().split()]
    logsum = 0
    prod = 1
    for a in As:
        logsum += math.log10(a)
        prod *= a

    if logsum > 18:
        print('-1')
        return

    print(prod)

    # As = []
    # for i in range(N):
    #     tmp = int(input())
    #     As.append(tmp)

    return 0

if __name__ == '__main__':
    sys.exit(main())
Problem p02603 (python)

Fixed submission:
N=int(input())
A=list(map(int, input().split()))
#1000

#1300
#35
L=[0]*N
if A[0]<A[1]:
    L[0]=1
    up=1
elif A[0]==A[1]:
    up=0
else:
    up=-1

for i in range(1,N):
    if i==N-1:
        L[i]=-1
        break
    if up==1:
        if A[i]>A[i+1]:
            up=-1
            L[i]=-1
    elif up==0:
        if A[i-1]<A[i]:
            L[i-1]=1
            up=1
        elif A[i-1]>A[i]:
            up=-1

    else:
        if A[i]<A[i+1]:
            up=1
            L[i]=1

#print(L)
now=1000
have=0
for i in range(len(L)):
    l=L[i]
    if l==1:
        have=now//A[i]
        now=now%A[i]
    elif l==-1:
        now+=A[i]*have
        have=0
print(now)

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int, input().split()))
#1000

#1300
#35
L=[0]*N
if A[0]<A[1]:
    L[0]=1
    up=1
elif A[0]==A[1]:
    up==0
else:
    up=-1

for i in range(1,N):
    if i==N-1:
        L[i]=-1
        break
    if up==1:
        if A[i]>A[i+1]:
            up=-1
            L[i]=-1
    elif up==0:
        if A[i-1]<A[i]:
            L[i-1]=1
            up=1
        elif A[i-1]>A[i]:
            up=-1

    else:
        if A[i]<A[i+1]:
            up=1
            L[i]=1

#print(L)
now=1000
have=0
for i in range(len(L)):
    l=L[i]
    if l==1:
        have=now//A[i]
        now=now%A[i]
    elif l==-1:
        now+=A[i]*have
        have=0
print(now)
Problem p02647 (python)

Fixed submission:
#import numpy as np
N,K=map(int, input().split())

A=list(map(int, input().split()))
#K=min(K, 30)
for k in range(K):
    B=[0]*(N+1)
    for i in range(N):
        a=A[i]
        start=max(0,i-a)
        end=min(N,i+a+1)
        B[start]+=1
        B[end]-=1
    #B=np.array(B)
    A=[0]*N
    A[0]=B[0]
    for i in range(1,N):
        A[i]=A[i-1]+B[i]

    if min(A)==N:
        break
    #print(B)
    
    
print(*A)

Buggy submission (verdict: Runtime Error):
import numpy as np
from numba import njit

@njit
def main():

    N,K=map(int, input().split())

    A=list(map(int, input().split()))

    for k in range(K):
        B=[0]*(N+1)
        for i in range(N):
            a=A[i]
            start=max(0,i-a)
            end=min(N,i+a+1)
            B[start]+=1
            B[end]-=1
        B=np.array(B)
        A=np.cumsum(B)[:N]
        if A.min()==N:
            break   
    print(*A)

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

Fixed submission:
N,K=map(int, input().split())
A=list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**8)
import numpy as np
done=np.array([0]*N)
X=[0]*2
L=[]
F=[True]
def dfs(start, steps):
    done[start-1]+=1
    L.append(start)
    if done[start-1]==3:
        X[1]=steps
        return
    elif done[start-1]==2 and F[0]:
        X[0]=steps
        F[0]=False
    nex=A[start-1]
    dfs(nex, steps+1)
dfs(1,0)
#print(done)
#print(X)
#print(L)
first=L[:X[0]-(X[1]-X[0])]
sec=L[X[0]-(X[1]-X[0]):X[0]-(X[1]-X[0])+(X[1]-X[0])]
#print(first, sec)
if len(first)==0:
    first=sec
if K+1<=len(first):
    print(first[K])
else:
    K=K-len(first)
    print(sec[(K%len(sec))])

Buggy submission (verdict: Runtime Error):
N,K=map(int, input().split())
A=list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**8)
import numpy as np
done=np.array([0]*N)
X=[0]*2
L=[]
F=[True]
def dfs(start, steps):
    done[start-1]+=1
    L.append(start)
    if done[start-1]==3:
        X[1]=steps
        return
    elif done[start-1]==2 and F[0]:
        X[0]=steps
        F[0]=False
    nex=A[start-1]
    dfs(nex, steps+1)
dfs(1,0)
#print(done)
#print(X)
#print(L)
first=L[:X[0]-(X[1]-X[0])]
sec=L[X[0]-(X[1]-X[0]):X[0]-(X[1]-X[0])+(X[1]-X[0])]
#print(first, sec)
if len(first)==0:
    first=sec
if K-1<=len(first):
    print(first[K])
else:
    K=K-len(first)
    print(sec[(K%len(sec))])

Buggy submission (verdict: Runtime Error):
N,K=map(int, input().split())
A=list(map(int, input().split()))
import sys
sys.setrecursionlimit(10**6)
import numpy as np
done=np.array([0]*N)
X=[0]*2
L=[]
F=[True]
def dfs(start, steps):
    done[start-1]+=1
    L.append(start)
    if done[start-1]==3:
        X[1]=steps
        return
    elif done[start-1]==2 and F[0]:
        X[0]=steps
        F[0]=False
    nex=A[start-1]
    dfs(nex, steps+1)
dfs(1,0)
#print(done)
#print(X)
#print(L)
first=L[:X[0]-(X[1]-X[0])]
sec=L[X[0]-(X[1]-X[0]):X[0]-(X[1]-X[0])+(X[1]-X[0])]
#print(first, sec)
if len(first)==0:
    first=sec
if K-1<=len(first):
    print(first[K])
else:
    K=K-len(first)
    print(sec[(K%len(sec))])
Problem p02691 (python)

Fixed submission:
N=int(input())
A=list(map(int, input().split()))

L=[0]*(10**6)
for i in range(N):
    if i-A[i]>=0:
        L[i-A[i]]+=1
ans=0
for i in range(N):
    tall=A[i]
    if tall+i<len(L):
        ans+=L[tall+i]
print(ans)
#print(L[:10])

Buggy submission (verdict: Runtime Error):
N=int(input())
A=list(map(int, input().split()))

L=[0]*(10**6)
for i in range(N):
    if i-A[i]>=0:
        L[i-A[i]]+=1
ans=0
for i in range(N):
    tall=A[i]
    ans+=L[tall+i]
print(ans)
#print(L[:10])
Problem p02713 (python)

Fixed submission:
import math

from functools import reduce
K=int(input())

def gcd_list(numbers):
  return reduce(math.gcd, numbers)


count=0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      if i==j and j==k and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list([i,j,k])

print(count)

Buggy submission (verdict: Runtime Error):
import fractions

from functools import reduce
K=int(input())

def gcd_list(a,b,c):
  return gcd(gcd(a,b),gcd(b,c))


count=0
for i in range(1,K+1):
  for j in range(i,K+1):
    for k in range(j,K+1):
      
      if i==j and j==k and k==i:
        n=1
      elif i==j or j==k or k==i:
        n=3
      else:
        n=6
      count+=n*gcd_list(i,j,k)

print(count)
Problem p02714 (python)

Fixed submission:
N=int(input())
S=input()
count=0
R=0
G=0
B=0
for i in range(N):
  if S[i]=="R":
    R+=1
  if S[i]=="G":
    G+=1
  if S[i]=="B":
    B+=1
count=0
for i in range(N-2):
  for j in range(i,N-1):
    k=j+j-i
    try:
      if S[i]!=S[j] and S[j]!=S[k] and S[k]!=S[i]:
        count+=1
    except:
      pass
    
print(R*G*B-count)

Buggy submission (verdict: Runtime Error):
from numba import jit

N=int(input())
S=input()
R=0
G=0
B=0
for i in range(N):
    s=S[i]
    if s=="R":
        R+=1
    elif s=="G":
        G+=1
    else:
        B+=1
ans=R*G*B

@njit
def calc(ans):
    for i in range(N-2):
        for j in range(i+1, N-1):
            if 0<=j+j-i<N:
                si=S[i]
                sj=S[j]
                sk=S[2*j-i]
                if si!=sj and sj!=sk and sk!=si:
                    ans-=1
    return ans
print(calc(ans))