Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import math
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9+7

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    ans = 0
    for d in range(40,-2,-1):# X, Kは高々40bit
        # dはXとKのbitが初めて一致しない桁（全探索している）
        if d != -1 and not (K & (1<<d)):# Kが0の桁はdの候補から外れる
            continue

        tmp = 0
        for e in range(40,-1,-1):# 上の桁から見ていく
            mask = 1<<e# e桁目の数字だけ
            num = 0
            # 配列Aのそれぞれのe桁目のbitの和
            for i in range(N):
                if A[i] & mask:
                    num += 1
            
            if e > d:
                if K & mask:# e桁目が1なので元が0の数だけ立ち上がる
                    tmp += mask*(N-num)
                else:
                    tmp += mask*num
            elif e == d:# K==1よりX==0, よって元が1の数だけ立ち上がる
                tmp += mask*num
            else:# Xが自由に選べるから立ち上がる数が大きくなるようにGreedyに取れる
                tmp += mask*max(num,N-num)
        ans = max(ans,tmp)

    print(ans)

if __name__ == "__main__":
    main()


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

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    ans = 0
    for d in range(40,-2,-1):# X, Kは高々40bit
        # dはXとKのbitが初めて一致しない桁（全探索している）
        if d != -1 and not (K & (1<<d)):# Kが0の桁はdの候補から外れる
            continue

        tmp = 0
        for e in range(40,-1,-1):# 上の桁から見ていく
            mask = 1<<e# e桁目の数字だけ
            num = 0
            # 配列Aのそれぞれのe桁目のbitの和
            for i in range(N):
                if A[i] & mask:
                    num += 1
            
            if e > d:
                if K & mask:# e桁目が1なので元が0の数だけ立ち上がる
                    tmp += mask*(N-num)
                else:
                    tmp += mask*num
            elif e == d:# K==1よりX==0, よって元が1の数だけ立ち上がる
                tmp += mask*num
            else:# Xが自由に選べるから立ち上がる数が大きくなるようにGreedyに取れる
                tmp += mask*max(num,N-num)
        ans = max(ans,tmp)

    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = [input() for i in range(N)]
ans = 0
for i in range(N):
  ans += len(set(A[0][i],A[1][i],A[2][i]))-1
print(ans)

Fixed submission:
N = int(input())
A = [input() for i in range(3)]
ans = 0
for i in range(N):
  ans += len(set([A[0][i],A[1][i],A[2][i]]))-1
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A, B = map(int, input().split())
P = list(map(int, input().split()))
a = len(filter(lambda x: x<=A,P))
b = len(filter(lambda x: A<x<=B,P))
c = len(filter(lambda x: B<x,P))
print(min(a,b,c))

Fixed submission:
N = int(input())
A, B = map(int, input().split())
P = list(map(int, input().split()))
a = len([*filter(lambda x: x<=A,P)])
b = len([*filter(lambda x: A<x<=B,P)])
c = len([*filter(lambda x: B<x,P)])
print(min(a,b,c))
Buggy python submission (verdict: Runtime Error):
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()

Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i-1]-h[i]))
        if i < N-1:
            dp[i+2] = min(dp[i+2],dp[i]+abs(h[i-1]-h[i+1]))
    print(dp[N])

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(2,N+1):
        dp[i] = min(dp[i-j]+abs(h[i-1]-h[i-j-1]) for j in range(1,min(K,i)+1))
    print(dp[N])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,min(K,N-i)+1):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()

Fixed submission:
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, K = map(int, input().split())
    h = list(map(int, input().split()))
    INF = 10**12
    dp = [INF] * (N+1)
    dp[1] = 0
    for i in range(1,N):
        for j in range(1,K+1):
            if i+j > N:
                break
            else:
                dp[i+j] = min(dp[i+j],dp[i]+abs(h[i-1]-h[i+j-1]))
    print(dp[N])

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, W = map(int, input().split())
    dp = [0]*(W+1)

    for _ in range(N):
        w, v = map(int, input().split())
        for j in range(W,-1,-1):# 後ろから更新しないとダメ
            if j - w >= 0:
                dp[j] = max(dp[j], dp[j-w]+v)
    print(dp[W])    

if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, W = map(int, input().split())
    w = [None] * N
    v = [None] * N

    for i in range(N):
        w[i], v[i] = map(int, input().split())
    
    dp = [[0]*(N+1) for _ in range(W+1)]
    for i in range(1,W+1):
        for j in range(N):
            if i - w[j] < 0:
                dp[i][j+1] = dp[i][j]
            else:
                dp[i][j+1] = max(dp[i][j], dp[i-w[j]][j]+v[j])
    print(dp[W][N])    

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, W = map(int, input().split())
    w = [None] * N
    v = [None] * N

    for i in range(N):
        w[i], v[i] = map(int, input().split())
    
    dp = [[0]*(N+1) for _ in range(W+1)]
    for i in range(1,W+1):
        for j in range(N):
            if i - w[j] < 0:
                dp[i][j+1] = dp[i][j]
            else:
                dp[i][j+1] = max(dp[i][j], dp[i-w[j]][j]+v[j])
    print(dp[W][N])    

if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, W = map(int, input().split())
    w = [None] * N
    v = [None] * N

    for i in range(N):
        w[i], v[i] = map(int, input().split())
    
    dp = [[0]*(N+1) for _ in range(W+1)]
    for i in range(1,W+1):
        for j in range(N):
            if i - w[j] < 0:
                dp[i][j+1] = dp[i][j]
            else:
                dp[i][j+1] = max(dp[i][j], dp[i-w[j]][j]+v[j])
    print(dp[W][N])    

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, W = map(int, input().split())
    V = N * 10**3
    INF = 10**12
    dp = [INF] * (V+1)# dp[i]: 価値の総和をiにするための重さの最小値
    dp[0] = 0

    for _ in range(N):
        w, v = map(int, input().split())
        for j in range(V,-1,-1):
            if j-v >= 0:
                dp[j] = min(dp[j],dp[j-v]+w)
    # print(dp)
    ans = -1
    for i in range(V+1):
        if dp[i] <= W:
            ans = i
    print(ans)

if __name__ == "__main__":
    main()


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

def main():
    N, W = map(int, input().split())
    V = N * 10**3
    INF = 10**12
    dp = [INF] * (V+1)# dp[i]: 価値の総和をiにするための重さの最小値
    dp[0] = 0

    for _ in range(N):
        w, v = map(int, input().split())
        for j in range(V,-1,-1):
            if j-v >= 0:
                dp[j] = min(dp[j],dp[j-v]+w)
    # print(dp)
    ans = -1
    for i in range(V+1):
        if dp[i] <= W:
            ans = i
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

s = input()
t = input()
N = len(s)
M = len(t)
ans = []

def print_lcs(dp,i,j):
    global ans
    if i == 0 or j == 0:
        return
    if s[i-1] == t[j-1]:
        print_lcs(dp,i-1,j-1)
        ans.append(s[i-1])
    else:
        if dp[i-1][j] >= dp[i][j-1]:
            print_lcs(dp,i-1,j)
        else:
            print_lcs(dp,i,j-1)


def main():
    dp = [[0]*(M+1) for _ in range(N+1)]
    
    for i in range(N):
        for j in range(M):
            if s[i] == t[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
    
    print_lcs(dp,N,M)
    print(''.join(ans))

if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
import sys
sys.setrecursionlimit(10**6)

s = input()
t = input()
N = len(s)
M = len(t)
ans = []

def print_lcs(dp,i,j):
    global ans
    if i == 0 or j == 0:
        return
    if s[i-1] == t[j-1]:
        print_lcs(dp,i-1,j-1)
        ans.append(s[i-1])
    else:
        if dp[i-1][j] >= dp[i][j-1]:
            print_lcs(dp,i-1,j)
        else:
            print_lcs(dp,i,j-1)


def main():
    dp = [[0]*(M+1) for _ in range(N+1)]
    
    for i in range(N):
        for j in range(M):
            if s[i] == t[j]:
                dp[i+1][j+1] = dp[i][j] + 1
            else:
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
    
    print_lcs(dp,N,M)
    print(''.join(ans))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, *A = map(int, open(0))
    A.sort()
    ans = 0
    if N % 2 == 1:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]+A[N//2+1]
    else:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]-A[N//2-1]
    

    ans2 = 0
    if N % 2 == 1:
        ans2 += sum(A[N//2+1:])*2 - sum(A[:N//2+1])*2
        ans2 += A[N//2]+A[N//2-1]
    else:
        ans2 += sum(A[N//2+1:])*2 - sum(A[:N//2+1])*2
        ans2 += A[N//2]-A[N//2+1]

    print(max(ans,ans2))

if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N, *A = map(int, open(0))
    A.sort()
    ans = 0
    if N % 2 == 1:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]+A[N//2+1]
    else:
        ans += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans -= A[N//2]-A[N//2-1]
    

    ans2 = 0
    if N % 2 == 1:
        ans2 += sum(A[N//2+1:])*2 - sum(A[:N//2+1])*2
        ans2 += A[N//2]+A[N//2-1]
    else:
        ans2 += sum(A[N//2:])*2 - sum(A[:N//2])*2
        ans2 += A[N//2-1]-A[N//2]

    print(max(ans,ans2))

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sympy
from bisect import bisect_left

def main():
    N, M = map(int, input().split())
    ans = sympy.divisors(M)
    i = bisect_left(ans,N)
    print(M//ans[i])

if __name__ == "__main__":
    main()

Fixed submission:
#!/usr/bin/env python3
from bisect import bisect_left
from heapq import heappush

def cal_divisors(N):
    divisors = []
    i = 1
    while i*i <= N:
        if N % i == 0:
            divisors.append(i)
            if i != N // i:
                divisors.append(N//i)
        i += 1
    divisors.sort()
    return divisors

def main():
    N, M = map(int, input().split())
    ans = cal_divisors(M)
    # print(ans)
    i = bisect_left(ans,N)
    print(M//ans[i])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3


def main():
    N, M = map(int, input().split())
    ans = 0
    for i in range(1,M+1):
        if M%i==0:
            if M//i >= N:
                ans = i
                # print(ans)
            else:
                break
    print(ans)

if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3
from bisect import bisect_left
from heapq import heappush

def cal_divisors(N):
    divisors = []
    i = 1
    while i*i <= N:
        if N % i == 0:
            divisors.append(i)
            if i != N // i:
                divisors.append(N//i)
        i += 1
    divisors.sort()
    return divisors

def main():
    N, M = map(int, input().split())
    ans = cal_divisors(M)
    # print(ans)
    i = bisect_left(ans,N)
    print(M//ans[i])

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
A,B = map(int, input(),split())
if A == 2 or B == 2:
  print('No')
else:
  print('Yes')

Fixed submission:
A,B = map(int, input().split())
if A == 2 or B == 2:
  print('No')
else:
  print('Yes')
Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
import math

def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x = [abs(i - X) for i in x]
    if len(x) == 1:
        print(x[0])
        exit()
    
    ans = math.gcd(x[0], x[1])
    for i in range(2,N):
        ans = math.gcd(ans,x[i])

    print(ans)

if __name__ == '__main__':
    main()


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

def main():
    N, X = map(int, input().split())
    x = list(map(int, input().split()))
    x = [abs(i - X) for i in x]
    if len(x) == 1:
        print(x[0])
        exit()
    
    ans = fractions.gcd(x[0], x[1])
    for i in range(2,N):
        ans = fractions.gcd(ans,x[i])

    print(ans)

if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
N=int(input())
ans=0
for i in range(N+1):
  count=0
  for j in range(i+1):
    if i%j==0:
      count+=1
  if i%2==1 and count==8:
    ans+=1
print(ans)

Fixed submission:
N=int(input())
ans=0
for i in range(1,N+1):
  count=0
  for j in range(1,i+1):
    if i%j==0:
      count+=1
  if i%2==1 and count==8:
    ans+=1
print(ans)
Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if (A > 9 or B > 9):
    print(':(')
else:
    print('Yay!')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if (A > 9 or B > 9):
    print(':(')
else:
    print('Yay!')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A>9 or B>9:
  print ':('
else:
  print 'Yay!'

Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if ((A > 9) or (B > 9)):
    print(':(')
else:
    print('Yay!')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if ((A < 9) and (B < 9)):
    print('Yay!')
else:
    print(':(')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if (A < 9) and (B < 9):
    print('Yay!')
else:
    print(':(')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A <9 and B < 9:
    print('Yay!')
else:
    print(':(')


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A <9 and B < 9:
    print('Yay!')
else:
    print(':(')

Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A <9 and B < 9:
    print("Yay!")
else:
    print(":(")


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')

Buggy python submission (verdict: Runtime Error):
A = int(input())
B = int(input())
if A <9 and B < 9:
    print("Yay!")
else:
    print(":(")


Fixed submission:
A, B = map(int, input().split())
if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
tmp = input()
count = 0
for i in range(N):
    tmp_l = int(tmp.split(' ')[i])
    while(tmp_l % 2 == 0):
        count += 1
        tmp_l = tmp_l / 2

print(count)


Fixed submission:
import math
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
  while a[i] % 2 == 0:
    ans += 1
    a[i] = a[i] // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
tmp = input()
count = 0
for i in range(N):
    tmp_l = int(tmp.split(' ')[i])
    while(tmp_l % 2 == 0):
        count += 1
        tmp_l = tmp_l / 2

print(count)


Fixed submission:
import math
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
  while a[i] % 2 == 0:
    ans += 1
    a[i] = a[i] // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
tmp = input()
a_list = []
for i in range(N):
    a_list.append(int(tmp.split(' ')[i]))

count = 0
for l in a_list:
    tmp_l = l
    while(tmp_l % 2 == 0):
        count += 1
        tmp_l = tmp_l / 2

print(count)


Fixed submission:
import math
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
  while a[i] % 2 == 0:
    ans += 1
    a[i] = a[i] // 2
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
tmp = input()
a_list = []
for i in range(N):
    a_list.append(int(tmp.split(' ')[i]))

# print(a_list)


count = 0
for l in a_list:
    tmp_l = l
    while(tmp_l % 2 == 0):
        count += 1
        tmp_l = tmp_l / 2

print(count)


Fixed submission:
import math
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
  while a[i] % 2 == 0:
    ans += 1
    a[i] = a[i] // 2
print(ans)
Buggy python submission (verdict: Runtime Error):
return "ABC" if int(input()) < 1000 else "ABD"

Fixed submission:
print('ABC') if int(input()) < 1000 else print('ABD')
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
N, C = map(int, input().split())

D = [None] * C
for i in range(C):
    D[i] = list(map(int, input().split()))

c = [None] * N
for i in range(N):
    c[i] = list(map(int, input().split()))

ans = 10**12

# 塗り分けの順列総数
for p in permutations(range(C),3):
    tmp = 0
    for i in range(N):
        if tmp > ans:
            break
        for j in range(N):
            tmp += D[c[i][j]-1][p[(i+j)%3]]
    ans = min(ans,tmp)

print(ans)

Fixed submission:
from itertools import permutations

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

D = [None] * C
for i in range(C):
    D[i] = list(map(int, input().split()))

c = [None] * N
for i in range(N):
    c[i] = list(map(int, input().split()))

# (i+j)%3 = 0,1,2 のそれぞれで初期マスの色をカウント
t = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        t[(i+j)%3][c[i][j]-1] += 1

ans = 10**12

# 塗り分けの順列総数
for p in permutations(range(C),3):
    tmp = 0
    for i in range(C):
        tmp += D[i][p[0]] * t[0][i]
        tmp += D[i][p[1]] * t[1][i]
        tmp += D[i][p[2]] * t[2][i]
    ans = min(ans,tmp)

print(ans)

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

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

D = [None] * C
for i in range(C):
    D[i] = list(map(int, input().split()))

c = [None] * N
for i in range(N):
    c[i] = list(map(int, input().split()))

ans = 10**12

# 塗り分けの順列総数
for p in permutations(range(C),3):
    tmp = 0
    for i in range(N):
        for j in range(N):
            tmp += D[c[i][j]-1][p[(i+j)%3]]
    ans = min(ans,tmp)

print(ans)

Fixed submission:
from itertools import permutations

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

D = [None] * C
for i in range(C):
    D[i] = list(map(int, input().split()))

c = [None] * N
for i in range(N):
    c[i] = list(map(int, input().split()))

# (i+j)%3 = 0,1,2 のそれぞれで初期マスの色をカウント
t = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        t[(i+j)%3][c[i][j]-1] += 1

ans = 10**12

# 塗り分けの順列総数
for p in permutations(range(C),3):
    tmp = 0
    for i in range(C):
        tmp += D[i][p[0]] * t[0][i]
        tmp += D[i][p[1]] * t[1][i]
        tmp += D[i][p[2]] * t[2][i]
    ans = min(ans,tmp)

print(ans)
Buggy python submission (verdict: Runtime Error):
import math
a,b,c,d = map(int,input().split())
print('Yes') if (math.abs(a-b)<=d and math.abs(b-c) <= d) or math.abs(a-c) <= d else print('No')

Fixed submission:
a,b,c,d = map(int,input().split())
print('Yes') if (abs(a-b)<=d and abs(b-c) <= d) or abs(a-c) <= d else print('No')
Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3


def main():
    s = input()
    N = len(s)
    K = int(input())
    X = set()
    for i in range(1,N+1):
        for j in range(N-i+1):
            X.add(s[j:j+i])
    print(sorted(X)[K-1])

if __name__ == "__main__":
    main()


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


def main():
    s = input()
    N = len(s)
    K = int(input())
    X = set()
    for i in range(1,K+1):
        for j in range(N-i+1):
            X.add(s[j:j+i])
    print(sorted(X)[K-1])

if __name__ == "__main__":
    main()