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

dic = {}
n, m = map(int, input().split())
for _ in range(n):
  a, b = map(int, input().split())
  if dic.get(a) is not None:
    dic[a].append(-b)
  else:
    dic[a] = [-b,]

ans = 0
bite = []
for i in range(1, m+1):
  if dic.get(i) is not None:
    bite = bite + dic[i]
  if len(bite) > 0:
    heapq.heapify(bite)
    ans += heapq.heappop(bite)
print(ans*-1)

Fixed submission:
import heapq

dic = {}
n, m = map(int, input().split())
for _ in range(n):
  a, b = map(int, input().split())
  if dic.get(a) is not None:
    dic[a].append(-b)
  else:
    dic[a] = [-b,]

ans = 0
bite = []
for i in range(1, m+1):
  if dic.get(i) is not None:
    #bite = bite + dic[i]
    for val in dic[i]:
      heapq.heappush(bite, val)
  if len(bite) > 0:
    #heapq.heapify(bite)
    ans += heapq.heappop(bite)
print(ans*-1)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

ab.sort()

dp = [0]*(m+1)
for i in range(1, m+1):
  bmax = 0
  bidx = -1
  for idx, [a, b] in enumerate(ab):
    if a > i:
      break
    if b > bmax:
      bmax = b
      bidx = idx
  dp[i] = dp[i-1] + bmax
  if bidx != -1:
    ab.pop(bidx)
print(dp[-1])

Fixed submission:
import heapq

dic = {}
n, m = map(int, input().split())
for _ in range(n):
  a, b = map(int, input().split())
  if dic.get(a) is not None:
    dic[a].append(-b)
  else:
    dic[a] = [-b,]

ans = 0
bite = []
for i in range(1, m+1):
  if dic.get(i) is not None:
    #bite = bite + dic[i]
    for val in dic[i]:
      heapq.heappush(bite, val)
  if len(bite) > 0:
    #heapq.heapify(bite)
    ans += heapq.heappop(bite)
print(ans*-1)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]

for i in range(n):
  acp = a.copy()
  acp[i] = 0
  acp.sort()
  print(acp[-1])

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]

sorted_a = a.copy()
sorted_a.sort()
if sorted_a[-1] == sorted_a[-2]:
  for i in range(n):
    print(sorted_a[-1])
else:
  for i in range(n):
    if a[i] == sorted_a[-1]:
      print(sorted_a[-2])
    else:
      print(sorted_a[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
ans=0
for i in range(1,n+1):
  X=[]
  for j in range(1,i+1):
    if i%j==0:
      X.append(j)
  ans+=i*len(X)
print(ans)

Fixed submission:
n=int(input())
ans=0
for i in range(1,n+1):
  ans+=(i+i*(n//i))*(n//i)//2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
ans=0
for i in range(1,n+1):
  X=[]
  for j in range(1,i+1):
    if i%j==0:
      X.append(j)
  ans+=i*len(X)
print(ans)

Fixed submission:
n=int(input())
ans=0
for i in range(1,n+1):
  ans+=(i+i*(n//i))*(n//i)//2
print(ans)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
ans=[]
p=a[0]^a[1]^a[2]^a[3]
for i in range(n):
  k=p^a[i]
  ans.append(k)
print(*ans)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ans=[]
p=0
for i in range(n):
  p=p^a[i]
  
for i in range(n):
  k=p^a[i]
  ans.append(k)
print(*ans)
Buggy python submission (verdict: Runtime Error):
N=int(input())
A=[int(_) for _ in input().split()]
B=[]
for i in range(N):
  for j in range(N):
    if A[i]%A[j]==0:
      B.append(i)
C=([x for x in set(B) if B.count(x) < 2])
print(len(C))

Fixed submission:
A=[int(_) for _ in input().split()]
B=sum(A)
S=15-B
print(S)
Buggy python submission (verdict: Runtime Error):
x,y=map(int,input().split())
ans="No"
if a in range(x+1):
  b=x-a
  a*2+b*4==y:
    ans="Yes"
print(ans)

Fixed submission:
X,Y=map(int, input().split())
p=(4*X-Y)/2
q=(Y-2*X)/2
if p==int(p) and q==int(q) and p>=0 and q>=0:
  print("Yes")
else:
  print("No")
Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
A=[int(_) for _ in input().split()]
ans=1
for i in range(n):
  ans=ans*A[i]
if ans>10**18:
  print(-1)
else:
  print(ans)

Fixed submission:
n=int(input())
A=[int(_) for _ in input().split()]
ans=1
if A.count(0)>0:
  print(0)
else:
  for i in range(n):
    ans=ans*A[i]
    if ans>10**18:
      print(-1)
      exit()
  print(ans)
Buggy python submission (verdict: Runtime Error):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

n=int(input())
x=factorization(n)
if n=1:
  print(0)
  exit()
  
ans=0
for i in range(len(x)):
  p=x[i][1]
  j=1
  while True:
    if p>=j:
      p-=j
      ans+=1
      j+=1
    if p<j:
      break
print(ans)    
    




Fixed submission:
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])
    if arr==[]:
        arr.append([n, 1])
    return arr

n=int(input())
x=factorization(n)
ans=0

if n==1:
  print(0)
  exit()
for i in range(len(x)):
  p=x[i][1]
  j=1
  while True:
    if p>=j:
      p-=j
      ans+=1
      j+=1
    if p<j:
      break
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
a,b=map(int,input().split())
p=0
for i in range(a,b+1):
  p=p^i
print(p)

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

p=a-1
if (a-1)%4==0:
  p=a-1
elif (a-1)%4==1:
  p=1
elif (a-1)%4==2:
  p=(a-1)^1
else:
  p=0
  
if b%4==0:
  b=b
elif b%4==1:
  b=1
elif b%4==2:
  b=b^1
else:
  b=0
print(p^b)
Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
h=list(map(int,input().split()))
#dp[i]はiに来たときのコストの総和の最小値

dp=[float('inf')]*n
dp[0]=0

for i in range(1,n):
  for j in range(min(k,i)):
    dp[i]=min(dp[i],dp[i-j-1]+abs(h[i]-h[i-j-1]))

print(dp[n-1])

Fixed submission:
n,k=map(int,input().split())
h=list(map(int,input().split()))
#dp[i]はiに来たときのコストの総和の最小値

dp=[float('inf')]*n
dp[0]=0

for i in range(1,n):
  for j in range(min(k,i)):
    dp[i]=min(dp[i],dp[i-j-1]+abs(h[i]-h[i-j-1]))

print(dp[n-1])
Buggy python submission (verdict: Time Limit Exceeded):
a, b, c, k = map(int, input().split())

l = [1 for _ in range(a)] + [0 for _ in range(b)] + [-1 for _ in range(c)]
print(sum(l[:k]))

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

ans = 0
ans += min(a, k)
ans -= max(0, k - a - b) 

print(ans)
Buggy python submission (verdict: Runtime Error):
k = int(input())
a, b = map(int, input().split())
 
if ((a // k) + 1) * k <= b or a % k = 0:
  print("OK")
else:
  print("NG")

Fixed submission:
k = int(input())
a, b = map(int, input().split())
 
if ((a // k) + 1) * k <= b or a % k == 0:
  print("OK")
else:
  print("NG")
Buggy python submission (verdict: Time Limit Exceeded):
def a(n,k):
  ans = n
  while(True):
    if abs(ans-k) < ans:
      ans = abs(ans-k)
    else:
      break

  print(ans)
  
n, k = map(int, input().split())
a(n,k)

Fixed submission:
n, k = map(int, input().split())

d = n // k
ans = min(k-(n-k*d), (n-k*(d-1))-k)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())

ans = n
while(True):
  if abs(ans-k) < ans:
    ans = abs(ans-k)
  else:
    break

print(ans)

Fixed submission:
n, k = map(int, input().split())

d = n // k
ans = min(k-(n-k*d), (n-k*(d-1))-k)
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

for each in a:
  tmp = sum(dict1.values()) - dict1[each] + dict2[each]
  print(tmp)

Fixed submission:
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

s = sum(dict1.values())
for each in a:
  tmp = s - dict1[each] + dict2[each]
  print(tmp)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from math import factorial

def combinations_count(n, r):
  if n-r < 0:
    return 0
  else:
    return factorial(n) // (factorial(n-r) * factorial(r))

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

c = Counter(a)
d = {}
for each in c:
  d[each] = combinations_count(c[each], 2)
  
for each in a:
  tmp = sum(d.values()) - d[each]
  tmp += combinations_count(c[each]-1, 2)
  print(tmp)

Fixed submission:
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

s = sum(dict1.values())
for each in a:
  tmp = s - dict1[each] + dict2[each]
  print(tmp)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
from math import factorial

def combinations_count(n, r):
  if n-r < 0:
    return 0
  else:
    return factorial(n) // (factorial(n-r) * factorial(r))

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

c = Counter(a)
counts = c.most_common()
d = {}
for each in counts:
  d[each[0]] = combinations_count(each[1], 2)
  
for each in a:
  tmp = sum(d.values()) - d[each]
  tmp += combinations_count(c[each]-1, 2)
  print(tmp)

Fixed submission:
from collections import Counter

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

d = Counter(a)

dict1 = {}
dict2 = {}
for key in d:
  m = d[key]
  if m == 1:
    dict1[key] = 0
    dict2[key] = 0
  elif m == 2:
    dict1[key] = 1
    dict2[key] = 0
  else:
    dict1[key] = m*(m-1)//2
    dict2[key] = (m-1)*(m-2)//2

s = sum(dict1.values())
for each in a:
  tmp = s - dict1[each] + dict2[each]
  print(tmp)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

h, w = map(int, input().split())

board = [[(i+j+1) % 2 for i in range(w)] for j in range(h)]
print(np.sum(board))

Fixed submission:
import numpy as np

h, w = map(int, input().split())

if h == 1 or w == 1:
  print(1)
else:
  print(int(np.ceil(h*w/2)))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
P = list(map(int, input().split()))

count = 0
for i in range(1, n+1):
  if i in set(P):
    count += 1
    P = P[:P.index(i)]
  if len(P) == 0:
    break

print(count)

Fixed submission:
n = int(input())
P = list(map(int, input().split()))

count = 0
tmpmin = P[0]
for p in P:
  if p <= tmpmin:
    count += 1
    tmpmin = p

print(count)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

if not 1 in (a):
  print(-1)
else:
  ans = 1
  for i in range(1, n+1):
    idx = a.index(i)
    ans += len(a[:idx]) - 1
    a = a[idx:]
  
    if not i+1 in a:
      print(ans + len(a) - 1)
      break

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

ans = n
check = 1
for i in range(n):
  if a[i] == check:
    ans -= 1
    check += 1
if ans == n:
  ans = -1

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

n = int(input())
A = np.array(list(map(float, input().split())))

ans = [0 for _ in range(n)]
for i in range(n):
  tmpidx = np.argmin(A)
  A[tmpidx] = np.inf
  ans[i] = tmpidx+1

print(*ans)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

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

n = int(input())
A = np.array(list(map(float, input().split())))

ans = []
for i in range(n):
  tmpidx = np.argmin(A)
  A[tmpidx] = np.inf
  ans.append(tmpidx+1)

print(*ans)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

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

n = int(input())
A = np.array(list(map(int, input().split())))

ans = []
for i in range(n):
  tmpidx = np.argmin(A)
  A[tmpidx] = np.inf
  ans.append(tmpidx+1)

print(*ans)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

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

n = int(input())
A = np.array(list(map(int, input().split())))

ans = []
for i in range(n):
  tmpidx = np.argmin(A)
  A[tmpidx] = 10 ** 9
  ans.append(tmpidx+1)

print(*ans)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

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

n = int(input())
A = np.array(list(map(int, input().split())))

ans = []
for i in range(n):
  tmpidx = np.argmin(A)
  A[tmpidx] = 10 ** 9
  print(tmpidx+1)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

ans = []
for i in range(n):
  tmpidx = A.index(min(A))
  A[tmpidx] = 10 ** 9
  print(tmpidx+1)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = list(map(int, input().split()))

ans = []
for i in range(n):
  tmpidx = A.index(min(A))
  ans.append(tmpidx+1)
  A[tmpidx] = 10 ** 9
  
for a in ans:
  print(a)

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

ans = [0 for _ in range(n)]
for i in range(n):
  ans[A[i]-1] = i+1

print(*ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n, k, q = map(int, input().split())

scores = np.array([k for _ in range(n)])

for _ in range(q):
  a = int(input())
  scores -= 1
  scores[a-1] += 1

for s in scores:
  if s > 0:
    print("Yes")
  else:
    print("No")

Fixed submission:
n, k, q = map(int, input().split())

d = {}

for _ in range(q):
  a = int(input())
  d[a-1] = d.get(a-1, 0) + 1

scores = [k-q for _ in range(n)]
for key in d:
  scores[key] += d[key]

for s in scores:
  if s > 0:
    print("Yes")
  else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

n = int(input())
h = list(map(int, input().split()))

l = np.array([0 for _ in range(n)])
start = 0

for i in range(n-1):
  if h[i] >= h[i+1]:
    l[start:i+1] += 1
  else:
    start = i+1
    
print(max(l))

Fixed submission:
import numpy as np

n = int(input())
h = list(map(int, input().split()))

l = np.array([0 for _ in range(n)])
start = 0
count = 0
for i in range(n-1):
  if h[i] >= h[i+1]:
    count += 1
  else:
    l[start:i] += count
    start = i+1
    count = 0
else:
  l[start:] += count

print(max(l))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
ans = 0
for i in range(n):
  ans += i
  
print(ans)

Fixed submission:
n = int(input())
  
print(n*(n-1)//2)
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))

Max = max(A)
for i in range(n):
  if A[i] != Max:
    print(Max)
  else:
    print(max(set(A[:i] + A[i+1:])))

Fixed submission:
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))

first = max(A)
B = A.copy()
B.remove(first)
second = max(B)

for i in range(n):
  if A[i] != first:
    print(first)
  else:
    print(second)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))

Max = max(A)
for i in range(n):
  if A[i] != Max:
    print(Max)
  else:
    print(max(A[:i] + A[i+1:]))

Fixed submission:
n = int(input())
A = []
for _ in range(n):
  A.append(int(input()))

first = max(A)
B = A.copy()
B.remove(first)
second = max(B)

for i in range(n):
  if A[i] != first:
    print(first)
  else:
    print(second)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())

ans = set(range(1, n+1))

for i in range(m):
  l, r = map(int, input().split())
  ans = ans & set(range(l, r+1))

print(len(ans))

Fixed submission:
n, m = map(int, input().split())

maxl = 0
minr = 10**5
for i in range(m):
  l, r = map(int, input().split())
  maxl = max(maxl, l)
  minr = min(minr, r)

print(max(minr-maxl+1, 0))
Buggy python submission (verdict: Time Limit Exceeded):
r, g, b, n = map(int, input().split())

ans = 0
for i in range(1+n//r):
  for j in range(1+n//g):
    div = (n - g*j - r*i)//b
    mod = (n - g*j - r*i)%b
    if div >= 0 and mod == 0:
      ans += 1
        
print(ans)

Fixed submission:
r, g, b, n = map(int, input().split())

ans = 0
for i in range(1+n//r):
  tmp = n - r*i
  for j in range(1+tmp//g):
    mod = (n - g*j - r*i)%b
    if mod == 0:
      ans += 1
        
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import collections
import itertools

N = int(input())
S = input()

ans = 0

for i in range(1,N+1):
  tmp_s_list = list(itertools.combinations(S,i))
  tmp_s_dict = collections.Counter(tmp_s_list)
  for key in tmp_s_dict:
    if max(collections.Counter(key).values()) == 1:
      ans += tmp_s_dict[key]

print(int(ans % (1e9 + 7)))

Fixed submission:
import numpy as np

N = int(input())
S = input()

unique_list = list(np.unique(S))

ans = 1

for s in unique_list:
  tmp = S.count(s)
  ans = ans * (tmp + 1)
  
print(int((ans-1) % (10**9 + 7)))

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

N = int(input())
S = input()

ans = 0

for i in range(1,N+1):
  tmp_s_list = list(itertools.combinations(S,i))
  for s in tmp_s_list:
    tmp_count = collections.Counter(s)
    if max(tmp_count.values()) == 1:
      ans += 1

print(ans)

Fixed submission:
import numpy as np

N = int(input())
S = input()

unique_list = list(np.unique(S))

ans = 1

for s in unique_list:
  tmp = S.count(s)
  ans = ans * (tmp + 1)
  
print(int((ans-1) % (10**9 + 7)))

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

N = int(input())
S = input()

ans = 0

for i in range(1,N+1):
  tmp_s_list = list(itertools.combinations(S,i))
  for s in tmp_s_list:
    tmp_count = collections.Counter(s)
    if max(tmp_count.values()) == 1:
      ans += 1

print(ans)

Fixed submission:
import numpy as np

N = int(input())
S = input()

unique_list = list(np.unique(S))

ans = 1

for s in unique_list:
  tmp = S.count(s)
  ans = ans * (tmp + 1)
  
print(int((ans-1) % (10**9 + 7)))
Buggy python submission (verdict: Runtime Error):
s = input()

ans = 0
i = 1
while len(s[i:]) >= 1:
  before = s[i-1]
  tmp = s[i]
  if before != tmp:
    s = s[:i-1] + s[i+1:]
    ans += 2
    i -= 1
  else:
    i += 1

print(ans)

Fixed submission:
s = input()

print(2 * min(s.count("0"), s.count("1")))
Buggy python submission (verdict: Runtime Error):
s = input()
k = int(input())

tmp = 0
for each in s:
  if each > 1:
    print(each)
    break
  tmp += 1
  if tmp == k:
    print(1)
    break

Fixed submission:
s = input()
k = int(input())

tmp = 0
for each in s:
  if int(each) > 1:
    print(each)
    break
  tmp += 1
  if tmp == k:
    print(1)
    break
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

def dfs(A, a, n, ans):
  if len(A) == n:
    tmp = A + a
    if sum(tmp % 2) < len(A):
      ans += 1
    return ans
  
  for i in range(-1, 2):
    A = np.append(A, i)
    ans = dfs(A, a, n, ans)
    A = np.delete(A, -1)
    
  return ans

n = int(input())
a = np.array(list(map(int, input().split())))

print(dfs(np.array([]), a, n, 0))

Fixed submission:
import numpy as np

def dfs(A, a, n, ans):
  if len(A) == n:
    tmp = np.array(A) + a
    if sum(tmp % 2) < len(A):
      ans += 1
    return ans
  
  for i in range(-1, 2):
    A.append(i)
    ans = dfs(A, a, n, ans)
    A.pop()
    
  return ans

n = int(input())
a = np.array(list(map(int, input().split())))

print(dfs([], a, n, 0))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for _ in range(n)]

ans = 0
visited = set([0])
next_visit_idx = 0
while True:
  next_visit_idx = a[next_visit_idx] -1
  ans += 1
  if next_visit_idx == 1:
    print(ans)
    break
  if next_visit_idx in visited:
    print(-1)
    break

Fixed submission:
n = int(input())
a = [int(input()) for _ in range(n)]

ans = 0
visited = set([0])
next_visit_idx = 0
while True:
  next_visit_idx = a[next_visit_idx] -1
  ans += 1
  if next_visit_idx == 1:
    print(ans)
    break
  if next_visit_idx in visited:
    print(-1)
    break
    
  visited.add(next_visit_idx)
Buggy python submission (verdict: Runtime Error):
def judge_color(n):
  if 1 <= n <= 399:
    return "gray"
  elif 400 <= n <= 799:
    return "brown"
  elif 800 <= n <= 1199:
    return "green"
  elif 1200 <= n <= 1599:
    return "sky"
  elif 1600 <= n <= 1999:
    return "blue"
  elif 2000 <= n <= 2399:
    return "yellow"
  elif 2400 <= n <= 2799:
    return "orange"
  elif 2800 <= n <= 3199:
    return "red"
  else:
    return "free"

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

d = {}
for each in a:
  color = judge_color(each)
  d[color] = d.get(color, 0) + 1
  
c = d.get("free",0)
l = len(d)
if c == 0:
  print(l, l)
else:
  print(max(l-1,1) l-1+c)

Fixed submission:
def judge_color(n):
  if 1 <= n <= 399:
    return "gray"
  elif 400 <= n <= 799:
    return "brown"
  elif 800 <= n <= 1199:
    return "green"
  elif 1200 <= n <= 1599:
    return "sky"
  elif 1600 <= n <= 1999:
    return "blue"
  elif 2000 <= n <= 2399:
    return "yellow"
  elif 2400 <= n <= 2799:
    return "orange"
  elif 2800 <= n <= 3199:
    return "red"
  else:
    return "free"

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

d = {}
for each in a:
  color = judge_color(each)
  d[color] = d.get(color, 0) + 1
  
c = d.get("free",0)
l = len(d)
if c == 0:
  print(l, l)
else:
  print(max(l-1,1), l-1+c)
Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

dp = [[-1 for i in range(n+2)] for j in range(m+2)]
def find(i, j, sum):
    if sum >= k:
        return 0
    if i < len(a) and j < len(b) and dp[i][j] != -1:
        return dp[i][j]
    dp[i][j] = 0
    if j < len(b) and sum + b [j] <= k :
        dp[i][j] = 1 + find(i , j+1, sum+b[j])
    if i < len(a) and sum + a[i] <= k :
        dp[i][j] = max(dp[i][j], 1 + find(i+1, j, sum+a[i]))
    return dp[i][j]

print(find(0, 0, 0))


Fixed submission:

import sys

for a0 in range(1):
    n,m,x = input().strip().split(' ')
    n,m,x = [int(n),int(m),int(x)]
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.reverse()
    b.reverse()
    total = 0
    atmp = []
    for i in range(n):
        val = a.pop()
        if total + val > x:
            break
        total += val 
        atmp.append(val)

    max_count = len(atmp)
    cur_count = max_count
    while m:
        if total + b[-1] <= x:
            total += b.pop() 
            m -= 1
            cur_count += 1
            if cur_count > max_count:
                max_count = cur_count
            continue
        if not len(atmp):
            break
        aval = atmp.pop()
        total -= aval
        cur_count -= 1
    print (max_count)