Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
from collections import deque
H, W = map(int, input().split())
X = [input() for _ in range(H)]
maxloc = -10

def bfs(sx, sy, gx, gy):
  if(X[sx-1][sy-1] == '#' or X[gx-1][gy-1] == '#'):
    return -1
  else:
    dist = [[-1]*W for _ in range(H)]
    dist[sx-1][sy-1] = 0
    q = deque([(sx-1, sy-1)])
    while q:
      u, v = q.popleft()
      for i, j in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
        if not(0 <= u+i < H and 0 <= v+j < W):
          continue
    
        if(dist[u+i][v+j] >= 0 or X[u+i][v+j] == "#"):
          continue

        dist[u+i][v+j] = dist[u][v]+1
        q.append((u+i, v+j))

    return dist[gx-1][gy-1]

for i in range(H):
  for j in range(W):
    sx = i
    sy = j
    for k in range(i+1, H):
      for l in range(j+1, W):
        gx = k
        gy = l
        if(bfs(i+1, j+1, k+1, l+1) > maxloc):
          maxloc = bfs(i+1, j+1, k+1, l+1)
          
print(maxloc)


Fixed submission:
s = "abcdefghijklmnopqrstuvwxyz"
C = input()
for i in range(len(s)):
  if(s[i] == C):
    print(s[i+1])
Buggy python submission (verdict: Time Limit Exceeded):
def combination(n, k, MOD):
  if(k > n or k < 0):
    return 0
  frac = [1]*(n+1)
  for i in range(1, n+1):
    frac[i] = (frac[i-1]*i)%MOD
  return (frac[n]*pow(frac[k], MOD-2, MOD)*pow(frac[n-k], MOD-2, MOD))%MOD

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

ans = 0
for i in range(N):
  ans = (ans + A[i]*combination(i, K-1, MOD))%MOD
for i in range(N):
  ans = (ans - A[i]*combination(N-i-1, K-1, MOD))%MOD
  
print(ans)

Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
MOD = 10**9+7

#c[i] := (i)C(K-1)
c = [0] * (N+1)
#frac[i] = i!%MOD
frac = [1] * (N+1)
for i in range(1, N+1):
  frac[i] = (frac[i-1]*i)%MOD
for i in range(1, N+1):
  if(i >= K-1):
    c[i] = frac[i]*pow(frac[K-1], MOD-2, MOD)*pow(frac[i-K+1], MOD-2, MOD)%MOD

  
ans = 0
for i in range(N):
  ans = (ans + A[i]*c[i])%MOD
for i in range(N):
  ans = (ans - A[i]*c[N-i-1])%MOD
  
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
A = np.array(A)
ans = 0
POW = 1
for j in range(60):
  tmp = A & 1
  one = sum(tmp)
  zero = N - one
  ans = (ans + one * zero * POW) % MOD
  A >>= 1
  POW = (POW*2)%MOD
print(ans%MOD)

Fixed submission:
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
bit = [0] * 60
for i in range(N):
  n = bin(A[i])
  for j in range(len(n)-1, 1, -1):
    bit[len(n)-j-1] += int(n[j])
ans = 0
POW = 1
for j in range(60):
  ans = (ans +bit[j]*(N-bit[j])*POW)%MOD
  POW = (POW * 2)%MOD
print(ans%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
bit = [0] * 60
for i in range(N):
  n = A[i]
  cnt = 0
  while(n>=1):
    bit[cnt] += n%2
    n = n//2
    cnt += 1
ans = 0
POW = 1
for j in range(60):
  ans = (ans +bit[j]*(N-bit[j])*POW)%MOD
  POW = (POW * 2)%MOD
print(ans%MOD)

Fixed submission:
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
bit = [0] * 60
for i in range(N):
  n = bin(A[i])
  for j in range(len(n)-1, 1, -1):
    bit[len(n)-j-1] += int(n[j])
ans = 0
POW = 1
for j in range(60):
  ans = (ans +bit[j]*(N-bit[j])*POW)%MOD
  POW = (POW * 2)%MOD
print(ans%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
bit = [0] * 60
for i in range(N):
  n = A[i]
  cnt = 0
  while(n>=1):
    bit[cnt] += n%2
    n = n//2
    cnt += 1
ans = 0
POW = 1
for j in range(60):
  ans += (bit[j]*(N-bit[j])*POW)%MOD
  POW = (POW * 2)%MOD
print(ans%MOD)

Fixed submission:
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
bit = [0] * 60
for i in range(N):
  n = bin(A[i])
  for j in range(len(n)-1, 1, -1):
    bit[len(n)-j-1] += int(n[j])
ans = 0
POW = 1
for j in range(60):
  ans = (ans +bit[j]*(N-bit[j])*POW)%MOD
  POW = (POW * 2)%MOD
print(ans%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      n1 = 0
      n2 = 0
      n3 = 0
      p1 = 0
      p2 = 0
      p3 = 0
      while(p1 < len(S)):
        if(int(S[p1]) == i1):
          p2 = p1 + 1
          n1 = 1
          break
        p1 += 1
      while(p2 < len(S)):
        if(int(S[p2]) == i2):
          p3 = p2 + 1
          n2 = 1
          break
        p2 += 1
      while(p3 < len(S)):
        if(int(S[p3]) == i3):
          n3 = 1
          break
        p3 += 1
      if(n1 == 1 and n2 == 1 and n3 == 1):
        ans += 1
        
print(ans)


Fixed submission:
N = int(input())
S = input()
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      p = 1
      for f in range(N):
        if(p == 1 and S[f] == str(i1)):
          p = 2
        elif(p == 2 and S[f] == str(i2)):
          p = 3
        elif(p == 3 and S[f] == str(i3)):
          ans += 1
          break
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = 0
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      n1 = 0
      n2 = 0
      n3 = 0
      p1 = 0
      p2 = 0
      p3 = 0
      while(p1 < len(S)):
        if(int(S[p1]) == i1):
          p2 = p1 + 1
          n1 = 1
          break
        p1 += 1
      while(p2 < len(S)):
        if(int(S[p2]) == i2):
          p3 = p2 + 1
          n2 = 1
          break
        p2 += 1
      while(p3 < len(S)):
        if(int(S[p3]) == i3):
          n3 = 1
          break
        p3 += 1
      if(n1 == 1 and n2 == 1 and n3 == 1):
        ans += 1
        
print(ans)

Fixed submission:
N = int(input())
S = input()
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      p = 1
      for f in range(N):
        if(p == 1 and S[f] == str(i1)):
          p = 2
        elif(p == 2 and S[f] == str(i2)):
          p = 3
        elif(p == 3 and S[f] == str(i3)):
          ans += 1
          break
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = str(input())
list1 = []
cnt = 0
for x in range(len(S)):
  for y in range(x+1, len(S)):
    for z in range(y+1, len(S)):
      if (S[x]+S[y]+S[z] in list1) == False:
        cnt += 1
        list1.append(S[x]+S[y]+S[z])
print(cnt)

Fixed submission:
N = int(input())
S = input()
  
ans = 0
for i1 in range(10):
  for i2 in range(10):
    for i3 in range(10):
      p = 1
      for f in range(N):
        if(p == 1 and S[f] == str(i1)):
          p = 2
        elif(p == 2 and S[f] == str(i2)):
          p = 3
        elif(p == 3 and S[f] == str(i3)):
          ans += 1
          break
        
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int, input().split()))
ans = 0
for i in range(N):
  for j in range(i+1, N):
    for k in range(j+1, N):
      A = L[i] + L[j] + L[k]
      if(max(L[i],  L[j],  L[k]) < (A - max(L[i],  L[j],  L[k]))):
        ans += 1
        
print(ans)

Fixed submission:
from bisect import bisect_left
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N):
  for j in range(i+1, N):
    A = L[i] + L[j]
    highbound = bisect_left(L, A)
    ans += highbound - j - 1    
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = 0
for i in range(M):
  A[0] = A[0] // 2
  A.sort(reverse = True)
for i in range(N):
  ans += A[i]
print(ans)

Fixed submission:
import heapq
N, M = map(int, input().split())
a = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(a)

for i in range(M):
  mm = heapq.heappop(a)
  heapq.heappush(a, (-mm//2) * (-1))
  
print(-sum(a))
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9+7
S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
dp[0][0] = 1
p = 1
for i in range(l):
  if(S[i] != '?'):
    n = int(S[i])
    for j in range(13):
      dp[i+1][(j*10+n)%13] += dp[i][j]
  else:
    for k in range(10):
      for j in range(13):
        dp[i+1][(j*10+k)%13] += dp[i][j]
  for j in range(13):
    dp[i+1][j] = dp[i+1][j]%MOD
  p = (p*10)%MOD    
print(dp[l][5])

Fixed submission:
MOD = 10**9+7
S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
dp[0][0] = 1
p = 1
for i in range(l):
  if(S[i] != '?'):
    n = int(S[i])
    for j in range(13):
      dp[i+1][(j*10+n)%13] += dp[i][j]
  else:
    for k in range(10):
      for j in range(13):
        dp[i+1][(j*10+k)%13] += dp[i][j]
  for j in range(13):
    dp[i+1][j] = dp[i+1][j]%MOD
  p = (p*10)%MOD    
print(dp[l][5])

Buggy python submission (verdict: Runtime Error):
MOD = 10**9+7
S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
p = 10
dp[1][int(S[-1])] = 1
for i in range(1, l):
  if(S[l-i-1] != '?'):
    pi = (p * int(S[l-i-1])) % 13
    for j in range(13):
      dp[i+1][(j+pi)%13] = (dp[i+1][(j+pi)%13] + dp[i][j])%MOD
  else:
    for k in range(10):
      pi2 = p*k%13
      for j in range(13):
        dp[i+1][(j+pi2)%13] = (dp[i+1][(j+pi2)%13]+dp[i][j])%MOD
  p = (p*10)%MOD    
print(dp[l][5])

Fixed submission:
MOD = 10**9+7
S = input()
l = len(S)
dp = [[0] * 13 for _ in range(l+1)]
dp[0][0] = 1
p = 1
for i in range(l):
  if(S[i] != '?'):
    n = int(S[i])
    for j in range(13):
      dp[i+1][(j*10+n)%13] += dp[i][j]
  else:
    for k in range(10):
      for j in range(13):
        dp[i+1][(j*10+k)%13] += dp[i][j]
  for j in range(13):
    dp[i+1][j] = dp[i+1][j]%MOD
  p = (p*10)%MOD    
print(dp[l][5])
Buggy python submission (verdict: Time Limit Exceeded):
import bisect
N = int(input())
A = list()
for i in range(N):
  A.append(int(input()))
L = list()
for j in range(N-1, -1, -1):
  n = A[j]
  if(len(L) == 0):
    L.append(n)
  elif(L[-1] <= n):
    L.append(n)
  else:
    x = bisect.bisect_right(L, n)
    L[x] = n
  L.sort()
print(len(L))

Fixed submission:
import bisect
N = int(input())
A = list()
for i in range(N):
  A.append(int(input()))
L = list()
for j in range(N-1, -1, -1):
  n = A[j]
  if(len(L) == 0):
    L.append(n)
  elif(L[-1] <= n):
    L.append(n)
  else:
    x = bisect.bisect_right(L, n)
    L[x] = n
print(len(L))
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = set([int(input()) for _ in range(M)])
mod = 10**9 + 7
ans = [0] * (N+1)
ans[0] = 1
ans[1] = 1 if i not in a else 0
for i in range(2, N+1):
  ans[i] = (ans[i-1]+ans[i-2])%mod if i not in a else 0
print(ans[N])

Fixed submission:
N, M = map(int, input().split())
A = set([int(input()) for _ in range(M)])
mod = 10**9 + 7
ans = [0] * (N+1)
ans[0] = 1
ans[1] = 1 if 1 not in A else 0
for i in range(2, N+1):
  ans[i] = (ans[i-1]+ans[i-2])%mod if i not in A else 0
print(ans[N])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list()
for i in range(M):
  A.append(int(input()))
ans = [0] * (N+1)
for i in range(N+1):
  if(i not in A):
    if(i == 0 or i == 1):
      ans[i] = 1
    else:
      ans[i] = (ans[i-1]+ans[i-2])%1000000007
print(ans[N])

Fixed submission:
N, M = map(int, input().split())
A = set([int(input()) for _ in range(M)])
mod = 10**9 + 7
ans = [0] * (N+1)
ans[0] = 1
ans[1] = 1 if 1 not in A else 0
for i in range(2, N+1):
  ans[i] = (ans[i-1]+ans[i-2])%mod if i not in A else 0
print(ans[N])

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list()
for i in range(M):
  A.append(int(input()))
ans = list()
for i in range(N+1):
  if(i in A):
    ans.append(0)
  elif(i == 0 or i == 1):
    ans.append(1)
  else:
    ans.append((ans[i-1]+ans[i-2])%1000000007)
print(ans[N])

Fixed submission:
N, M = map(int, input().split())
A = set([int(input()) for _ in range(M)])
mod = 10**9 + 7
ans = [0] * (N+1)
ans[0] = 1
ans[1] = 1 if 1 not in A else 0
for i in range(2, N+1):
  ans[i] = (ans[i-1]+ans[i-2])%mod if i not in A else 0
print(ans[N])
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list()
C = list()
for i in range(M):
  x, y = map(int, input().split())
  B.append(x)
  C.append(y)
A.sort()
for i in range(M):
  for j in range(B[i]):
    if(A[j] < C[i]):
      A[j] = C[i]
    else:
      break
  A.sort()
print(sum(A))

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [[0]*2 for i in range(M+N)]
for i in range(M):
  B[i] = list(map(int, input().split()))
for i in range(N):
  B[M+i] = [1, A[i]]
B=sorted(B ,key=lambda x:x[1],reverse=True)
i = 0
cnt = 0
ans = 0
while(cnt < N):
  if(cnt + B[i][0] < N):
    ans += B[i][0] * B[i][1]
    cnt += B[i][0]
  else:
    ans += (N - cnt) * B[i][1]
    cnt = N
  i += 1
print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
S = input()
if(S[K-1] == 'A'):
  S[K-1] = 'a'
elif(S[K-1] == 'B'):
  S[K-1] = 'b'
else:
  S[K-1] = 'c'
print(S)

Fixed submission:
N, K = map(int, input().split())
S = input()
if(S[K-1] == 'A'):
  S = S[:(K-1)] + 'a' + S[K:]
elif(S[K-1] == 'B'):
  S = S[:(K-1)] + 'b' + S[K:]
else:
  S = S[:(K-1)] + 'c' + S[K:]
print(S)
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque

n = int(input())
graph = [[] for _ in range(n+1)]
for i in range(n-1):
  u, v, w = map(int, input().split())
  graph[u].append((v, w))
  graph[v].append((u, w))
  
parent = [0] * (n+1)
ans = [0] * (n+1)

q = deque([1])
while q:
  x = q.popleft()
  for y, w in graph[x]:
    if(parent[y] == x):
      continue
    else:
      parent[y] = x
      ans[y] = ans[x]^(w&1)
      q.append(y)
      
for i in range(1, n+1):
  print(ans[i])
      



Fixed submission:
from collections import deque

n = int(input())
graph = [[] for _ in range(n+1)]
for i in range(n-1):
  u, v, w = map(int, input().split())
  graph[u].append((v, w))
  graph[v].append((u, w))
  
parent = [0] * (n+1)
ans = [0] * (n+1)

q = deque([1])
while q:
  x = q.popleft()
  for y, w in graph[x]:
    if(y == parent[x]):
      continue
    parent[y] = x
    ans[y] = ans[x]^(w&1)
    q.append(y)
      
for i in range(1, n+1):
  print(ans[i])
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
zero = S.count('0')
one = S.count('1')
print(min(zero,one)*2)

Fixed submission:
S = input()
zero = S.count('0')
one = S.count('1')
print(min(zero,one)*2)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(int(m) for m in input().split())
ans = 1
for t in reversed(range(min(A) - 1)):
  B = list()
  for x in range(N):
    B.append(A[x] % (t+2))
    if B.count(0) != x+1:
      break
  if B.count(0) == N:
    ans = t+2
    break
print(ans)

Fixed submission:
def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if x % y == 0:
      return y
    else:
      while x % y != 0:
        z = x % y
        x = y
        y = z
      return y


N = int(input())
A = list(int(m) for m in input().split())
ans = A[0]
for i in range(N):
  ans = gcd(A[i], ans)
print(ans)
Buggy python submission (verdict: Runtime Error):
A, B = map(int,input().split())
cost = list()
m = B + 1
for i in range(A):
  x, y = map(int, input().split())
  if y < min:
    m = y
    cost.append(x)
print(min(cost) if m <= B else 'TLE')

Fixed submission:
A, B = map(int,input().split())
cost = list()
m = B + 1
for i in range(A):
  x, y = map(int, input().split())
  if y < m:
    cost.append(x)
print(min(cost) if len(cost) != 0 else 'TLE')
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list()
cnt = 0
A.append(input())
for i in range(N-1):
  x = input()
  if x in A:
    cnt = 1
  elif A[i][-1:] != x[0]:
    cnt = 1
  else:
    A.append(x)
print('Yes' if cnt == 0 else 'No')

Fixed submission:
N = int(input())
A = list()
cnt = 0
A.append(input())
for i in range(N-1):
  x = input()
  if x in A:
    cnt = 1
  elif A[i][-1:] != x[0]:
    cnt = 1
  A.append(x)
print('Yes' if cnt == 0 else 'No')
Buggy python submission (verdict: Time Limit Exceeded):
N, M, Q = map(int, input().split())
train = [[0]*(N+1) for _ in range(N+1)]

for i in range(M):
  a, b = map(int, input().split())
  train[a][b] += 1
  
for i in range(1, N+1):
  for j in range(1, N+1):
    train[i][j] += train[i][j-1]


for i in range(Q):
  ans = 0
  l, r = map(int, input().split())
  for j in range(l, r+1):
    ans += train[j][r]-train[j][l-1]
  print(ans)

Fixed submission:
N, M, Q = map(int, input().split())
train = [[0]*(N+1) for _ in range(N+1)]

for i in range(M):
  a, b = map(int, input().split())
  train[a][b] += 1
  
for i in range(1, N+1):
  for j in range(1, N+1):
    train[i][j] += train[i][j-1]


for i in range(Q):
  ans = 0
  l, r = map(int, input().split())
  for j in range(l, r+1):
    ans += train[j][r]-train[j][l-1]
  print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
INF = float('inf')
N, C = map(int, input().split())
D = list() #D[i][j]:=iからjに変える時の違和感
graph = list()
for i in range(C):
  D.append(list(map(int, input().split())))
for i in range(N):
  graph.append(list(map(int, input().split())))
ans = INF
for c0 in range(C):
  for c1 in range(C):
    for c2 in range(C):
      if(c0 == c1 or c1 == c2 or c2 == c0):
        continue
      tmp = 0
      for x in range(N):
        for y in range(N):
          p = (x+y+2)%3
          c = graph[x][y]-1
          if(p == 0):
            tmp += D[c][c0]
          elif(p == 1):
            tmp += D[c][c1]
          else:
            tmp += D[c][c2]
      ans = min(tmp, ans)
print(ans)

Fixed submission:
INF = float('inf')
N, C = map(int, input().split())
D = list() #D[i][j]:=iからjに変える時の違和感
graph = list()
for i in range(C):
  D.append(list(map(int, input().split())))
for i in range(N):
  graph.append(list(map(int, input().split())))
E = [[0] * C for _ in range(3)] #E[i][j]:=mod3でiの場所を全て色jにしたときの違和感
for c in range(C):
  for x in range(N):
    for y in range(N):
      p = (x+y+2)%3
      cxy = graph[x][y]-1
      E[p][c] += D[cxy][c]
F = [[[INF]*2 for _ in range(3)] for _ in range(3)] 
#F[i][j][k]:=mod3でiの場所の違和感の最小値第j+1位。k=0色番号, k=1最小値]
for i in range(3):
  for j in range(C):
    if(E[i][j] < F[i][0][1]):
      F[i][2] = F[i][1]
      F[i][1] = F[i][0]
      F[i][0] = [j, E[i][j]]
    elif(E[i][j] < F[i][1][1]):
      F[i][2] = F[i][1]
      F[i][1] = [j, E[i][j]]
    elif(E[i][j] < F[i][2][1]):
      F[i][2] = [j, E[i][j]]
      
ans = INF
for i in range(3):
  for j in range(3):
    for k in range(3):
      c0 = F[0][i][0]
      c1 = F[1][j][0]
      c2 = F[2][k][0]
      if(c0 != c1 and c1 != c2 and c2 != c0):
        ans = min(ans, F[0][i][1]+F[1][j][1]+F[2][k][1])
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
d = list()
for l in range(len(s)):
  for r in range(l,len(s)): #左からl番目、右からr番目までの文字列
    n = s[l:r+1]
    if n not in d:
      d.append(n)
    d.sort()
    d = d[:5]
print(d[K-1])

Fixed submission:
s = input()
K = int(input())
d = list()
for l in range(len(s)):
  for r in range(l,min(l+K+1, len(s))):#左からl番目、右からr番目までの文字列
    n = s[l:r+1]
    if n not in d:
      d.append(n)
    d.sort()
    d = d[:5]
print(d[K-1])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
d = list()
for l in range(len(s)):
  for r in range(l,len(s)): #左からl番目、右からr番目までの文字列
    n = s[l:r+1]
    if n not in d:
      d.append(n)
    d.sort()
    d = d[:6]
print(d[K-1])

Fixed submission:
s = input()
K = int(input())
d = list()
for l in range(len(s)):
  for r in range(l,min(l+K+1, len(s))):#左からl番目、右からr番目までの文字列
    n = s[l:r+1]
    if n not in d:
      d.append(n)
    d.sort()
    d = d[:5]
print(d[K-1])
Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
N, M = map(int, input().split())
P = list(map(int, input().split()))
graph = [[] for _ in range(N)]
can = [[] for _ in range(N)]
xy = list()
for i in range(M):
  x, y = map(int, input().split())
  graph[x-1].append(y-1)
  graph[y-1].append(x-1)
for i in range(N): 
  q = deque([i])
  while q:
    t = q.popleft()
    can[i].append(t)
    for next in graph[t]:
      if next not in can[i]:
        q.append(next)
ans = 0
for i in range(N):
  p = P[i] - 1
  if p in can[i]:
    ans += 1
print(ans)

Fixed submission:
class UnionFind(): 
    def __init__(self, n):
        self.n = n
        self.parents = [-1]*n 
        
    def find(self, x): 
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
            
    def union(self, x, y): 
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x,y = y,x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
    def same(self, x, y): 
        return self.find(x) == self.find(y)

N, M = map(int, input().split())
P = list(map(int, input().split()))
uf = UnionFind(N+1)
graph = [[] for _ in range(N)]
can = [[] for _ in range(N)]
xy = list()
for i in range(M):
  x, y = map(int, input().split())
  uf.union(x, y)
ans = 0
for i in range(1, N+1):
  if uf.same(i, P[i-1]):
    ans += 1
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import math

def primeornot(n):
  i = 2
  flag = 1
  if(n == 1):
    return 0
  while(i <= math.sqrt(n)):
    if(n % i == 0):
      flag = 0
      break
    else:
      i += 1
  return flag
    
Q = int(input())
for i in range(Q):
  l,r = map(int, input().split())
  ans = 0
  for n in range(l, r+1, 2):
    ans += ( primeornot(n) * primeornot((n+1)//2) )
  print(ans)

Fixed submission:
import bisect
Q = int(input())
maxn = 10 ** 5
intn = [True] * (maxn+1)
intn[0] = False
intn[1] = False
prime = list()
like2017 = list()
for n in range(2, maxn+1):
  if(intn[n] == False):
    continue
  prime.append(n)
  tmp = n
  while(tmp <= maxn):
    intn[tmp] = False
    tmp += n

for i in prime:
  q = (i+1)//2
  q_loc = bisect.bisect_left(prime, q)
  if(prime[q_loc] == q):
    like2017.append(i)

for i in range(Q):
  l, r = map(int, input().split())
  ln = bisect.bisect_left(like2017, l)
  rn = bisect.bisect_right(like2017, r)
  print(rn-ln)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
l1 = 0
l2 = 0
l4 = 0
for i in range(A):
  if(A[i] % 4 == 0):
    l4 += 1
  elif(A[i] % 2 == 0):
    l2 += 1
  else:
    l1 += 1
if(l4 < l1):
  print('No')
else:
  print('Yes')

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
l1 = 0
l2 = 0
l4 = 0
for i in range(len(A)):
  if(A[i] % 4 == 0):
    l4 += 1
  elif(A[i] % 2 == 0):
    l2 += 1
  else:
    l1 += 1
if(l2 == 0 and l4 + 1 == l1):
  print('Yes')
elif(l4 < l1):
  print('No')
else:
  print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = input().split()
for i in range(N):
    count = A.count(str(i+1))
    print(count)

Fixed submission:
import collections
N = int(input())
A = input().split()
count_dict = collections.Counter(A)
for i in range(N):
    count = count_dict[str(i+1)]
    print(count)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N = int(input())
arr = np.array(list(map(int, input().split())))
u, count = np.unique(arr, return_counts=True)
total = 0
for i in count:
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[u==i][0]+1))

Fixed submission:
import collections
N = int(input())
arr = list(map(int, input().split()))
count = collections.Counter(arr)
total = 0
for i in count.values():
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[i]+1))

Buggy python submission (verdict: Runtime Error):
import numpy as np
N = int(input())
arr = np.array(list(map(int, input().split())))
u, count = np.unique(arr, return_counts=True)
comb = []
for i in count:
    comb.append(i*(i-1)/2)
total = np.sum(comb)
comb = np.array(comb)
for i in arr:
    print(int(total- count[u==i][0]+1))

Fixed submission:
import collections
N = int(input())
arr = list(map(int, input().split()))
count = collections.Counter(arr)
total = 0
for i in count.values():
    total += (i*(i-1)/2)
for i in arr:
    print(int(total- count[i]+1))