Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
x, y = map(int, input().split())
list1 = [1, 3, 5, 7, 8, 10, 12]
list2 = [4, 6, 9, 11]
list3 = [2]
if (x in list1 and y in list1)　or (x in list2 and y in list2) or (x in list3 and y in list3):
  print('Yes')
else:
  print('No')
  

Fixed submission:
x, y = map(int, input().split())
list1 = [1, 3, 5, 7, 8, 10, 12]
list2 = [4, 6, 9, 11]
list3 = [2]
A = x in list1 and y in list1
B = x in list2 and y in list2
C = x in list3 and y in list3
if A == True or B == True or C == True:
  print('Yes')
else:
  print('No')
Buggy python submission (verdict: Runtime Error):
num = int(input())
hats = input()

rhat = len([hat for hat in fats if hat == "R"])
bhat = len([hat for hat in fats if hat == "B"])

if rhat > bhat:
    print("Yes")
else:
    print("No")

Fixed submission:
num = int(input())
hats = input()

rhat = len([hat for hat in hats if hat == "R"])
bhat = len([hat for hat in hats if hat == "B"])

if rhat > bhat:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int,input().split())
S = input()
AC = [False] * (N-1)
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)
for i in range(N-1):
  if S[i:i+2] == "AC":
    AC[i] =  True
for q in range(Q):
  sums = 0
  #print(S[lr[q][0]-1:lr[q][1]])
  for i in range(lr[q][1]-lr[q][0]):
    #print(S[(lr[q][0])+i-1])
    if AC[(lr[q][0])+i-1] == True:
      sums +=1
  print(sums)

Fixed submission:
N,Q = map(int,input().split())
S = input()
AC = [0] * N
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)
counts = 0
for i in range(N):
  if S[i:i+2] == "AC":
    counts +=1
  AC[i] =  counts
a =[0]
AC = a + AC
for q in range(Q):
  #print(str(lr[q][0])+" "+ str(lr[q][1]))
  print(AC[lr[q][1]-1] - AC[lr[q][0]-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = map(int,input().split())
S = input()
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)

for q in range(Q):
  Ss = S[lr[q][0]-1:lr[q][1]]
  sums = 0
  for i in range(len(Ss)-1):
    if Ss[i:i+2] == "AC":
      sums += 1
  print(sums)

Fixed submission:
N,Q = map(int,input().split())
S = input()
AC = [0] * N
buffer = []
lr =[]
for i in range(Q):
  buffer = list(map(int,input().split()))
  lr.append(buffer)
counts = 0
for i in range(N):
  if S[i:i+2] == "AC":
    counts +=1
  AC[i] =  counts
a =[0]
AC = a + AC
for q in range(Q):
  #print(str(lr[q][0])+" "+ str(lr[q][1]))
  print(AC[lr[q][1]-1] - AC[lr[q][0]-1])
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())

if n==1 and m==1:
    print(1)
elif n==1 and m!=1:
    print(m-2)
elif n!=1 and m==1:
    print(n-2)
else:
    print((n-2)*(m-2)

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

if n==1 and m==1:
    print(1)
elif n==1 and m!=1:
    print(m-2)
elif n!=1 and m==1:
    print(n-2)
else:
    print((n-2)*(m-2))
Buggy python submission (verdict: Runtime Error):
n=int(input())

ans=0

for i in range(n):
    l,r=map(int,input())
    ans+=r-l+1

print(ans)

Fixed submission:
n=int(input())

ans=0

for i in range(n):
    l,r=map(int,input().split())
    ans+=r-l+1

print(ans)

Buggy python submission (verdict: Runtime Error):
n=int(input())

ans=0

for i in range(N):
    l,r=map(int,input())
    ans+=r-l+1

print(ans)

Fixed submission:
n=int(input())

ans=0

for i in range(n):
    l,r=map(int,input().split())
    ans+=r-l+1

print(ans)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a={}
for i in range(n):
    b=int(input())
    if b in a[b]:
        del a[b]
    else:
        a[b]=1

print(len(a))

Fixed submission:
n=int(input())
a={}
for i in range(n):
    b=int(input())
    if b in a:
        del a[b]
    else:
        a[b]=1

print(len(a))
Buggy python submission (verdict: Runtime Error):
N = int(input())
happiness = []

for n in range(N):
    num = input().split()
    a = int(num[0])
    b = int(num[1])
    happiness.append([a + b, a, b])

happiness.reverse()

sum = 0
turn = 0

for n in range(N):
    if turn == 0:
        sum += happiness[n][1]
    else:
        sum -= happiness[n][2]
    turn ^= 1

print(sum)

Fixed submission:
line = input().split()
N = int(line[0])
A = int(line[1])
B = int(line[2])
print(min(A, B), max(A + B - N, 0))
Buggy python submission (verdict: Time Limit Exceeded):
# AtCoder Grand Contest 029
# December 15, 2018
#

#S = 'BWBWBW'
S = input()
N = len(S)

buf = []
for s in S:
  if s == 'B':
    buf.append(0)
  else:
    buf.append(1)

count = 0
flag = True
#print(buf)

while flag:
  for i in range(N-1):
    if buf[i] == 0 and buf[i+1] == 1:
      buf[i] = 1
      buf[i+1] = 0
      #print(buf)
      count += 1
      break
  else:
    flag = False

print(count)


Fixed submission:
S = input()
N = len(S)

count = 0
num = 0
idx = -1
while True:
  idx = S.find('W', idx + 1)
  if idx < 0:
    break
  count += idx - num
  num += 1

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
# AtCoder Grand Contest 029
# December 15, 2018
#

S = input()
count = 0

while True:
  if S.find('BW') >= 0:
    S = S.replace('BW', 'WB', 1)
    count += 1
  else:
    break

print(count)

Fixed submission:
S = input()
N = len(S)

count = 0
num = 0
idx = -1
while True:
  idx = S.find('W', idx + 1)
  if idx < 0:
    break
  count += idx - num
  num += 1

print(count)

Buggy python submission (verdict: Runtime Error):
# AtCoder Grand Contest 029
# December 15, 2018
#

S = input()
count = 0

while True:
  if S.find('BW') >= 0:
    S = S.replace('BW', 'WB', 1)
    count += 1
    print(S)
  else:
    break

print(count)

Fixed submission:
S = input()
N = len(S)

count = 0
num = 0
idx = -1
while True:
  idx = S.find('W', idx + 1)
  if idx < 0:
    break
  count += idx - num
  num += 1

print(count)
Buggy python submission (verdict: Runtime Error):
n=int(input())
s = str(input())
count=0
for i in range(n):
    if s[i]=='R':
        count+=1
        
if count > n-count:
    printr('Yes')
else:
    print('No')

Fixed submission:
n=int(input())
s = str(input())
count=0
for i in range(n):
    if s[i]=='R':
        count+=1
        
if count > n-count:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))


count=[0 for i in range(N)]
ind=0
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
      	ind+=1
        count[i+1]+=count[i+1]+ind

for i,j in lr:
    print(count[j-1]-count[i-1])


Fixed submission:
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))


count=[0 for i in range(N)]
ind=0
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
        ind+=1
    count[i+1]+=count[i+1]+ind

for i,j in lr:
    print(count[j-1]-count[i-1])

Buggy python submission (verdict: Runtime Error):
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))


count=[0 for i in range(N)]
ind=0
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
      	ind+=1
        count[i+1]+=ind

for i,j in lr:
    print(count[j-1]-count[i-1])


Fixed submission:
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))


count=[0 for i in range(N)]
ind=0
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
        ind+=1
    count[i+1]+=count[i+1]+ind

for i,j in lr:
    print(count[j-1]-count[i-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))

import numpy as np

count=np.array([0 for i in range(N)])
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
        count[i+1:]+=1

for i,j in lr:
    print(count[j-1]-count[i-1])

Fixed submission:
N,Q = list(map(int, input().split()))
S = str(input())
lr=[]
for i in range(Q):
    lr.append(list(map(int, input().split())))


count=[0 for i in range(N)]
ind=0
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":
        ind+=1
    count[i+1]+=count[i+1]+ind

for i,j in lr:
    print(count[j-1]-count[i-1])
Buggy python submission (verdict: Runtime Error):
N,M = list(map(int, input().split()))
lis=[]
for i in range(N):
    a,b = list(map(int, input().split()))
    lis.append([a,b])

lis.sort()

count=0
money=0
ind=0
while count+lis[ind][1]<=M:
    count+=lis[ind][1]
    money+=lis[ind][0]*lis[ind][1]
    ind+=1
    print(ind)
    

money+=(M-count)*lis[ind][0]
print(int(money))

Fixed submission:
N,M = list(map(int, input().split()))
lis=[]
for i in range(N):
    a,b = list(map(int, input().split()))
    lis.append([a,b])

lis.sort()

count=0
money=0
ind=0
while count+lis[ind][1]<M:
    count+=lis[ind][1]
    money+=lis[ind][0]*lis[ind][1]
    ind+=1
    #print(ind)
    

money+=(M-count)*lis[ind][0]
print(int(money))
Buggy python submission (verdict: Runtime Error):
N, M = list(map(int, input().split())) # 整数
a = []
for i in range(N):
    a.append(list(map(int, input().split())))
    

m_lis= [0 for i in range(M)]
for i in range(N):
    for j in range(a[i][0]):
        m_lis[a[i][j+1]]+=1
        
        

count=0
for i in range(N+1):
    if m_lis[i]==N:
        count+=1
        
print(count)

Fixed submission:
N, M = list(map(int, input().split())) # 整数
a = []
for i in range(N):
    a.append(list(map(int, input().split())))
    

m_lis= [0 for i in range(M+1)]
for i in range(N):
    for j in range(a[i][0]):
        m_lis[a[i][j+1]]+=1
        
        

count=0
for i in range(M+1):
    if m_lis[i]==N:
        count+=1
        
print(count)
Buggy python submission (verdict: Runtime Error):
K,A,B = list(map(int,input().split()))
if A>=B-2:
    answer = K+1
elif A+1>K:
    answer=K+1
else:
  # gutyoku
    first = A
    rest = K - (A-1) - 2
    if rest%2==0:
        answer = (rest/2+1)*B - (rest/2)*A
    else:
        #print('else')
        answer = ((rest-1)/2+1)*B - ((rest-1)/2)*A+1
    
print(int(answer))


Fixed submission:
N,K = list(map(int,input().split()))
if N >= 2*K -1:
  print("YES")
else:
  print("NO")

Buggy python submission (verdict: Runtime Error):
K,A,B = list(map(int,input().split()))
if A>=B-2:
    answer = K+1
else:
  # gutyoku
    first = A
    rest = K - A -1
    if rest%2==0:
        answer = (rest/2+1)*B - (rest/2)*A
    else:
        #print('else')
        answer = ((rest-1)/2+1)*B - ((rest-1)/2)*A+1
    
print(int(answer))


Fixed submission:
N,K = list(map(int,input().split()))
if N >= 2*K -1:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
import numpy as np
N,K =list(map(int, input().split()))
X = input().split()
for i in range(len(X)):
    X[i] = int(X[i])
X = np.array(X)

keta = len(bin(K))-2
matrix = np.zeros(keta*N).reshape(N,keta)
i_count=0
for i in X:
    for j in range(min(len(bin(i)[2:]),keta)):
        matrix[i_count,-j-1] = bin(i)[-j-1]
    i_count+=1
    
    

tot = np.zeros(keta)
for i in range(keta):
    tot[i]=sum(matrix[:,i])

ans = np.zeros(keta)
for i in range(len(tot)):
    if int(tot[i]) < N/2:
        ans[i]=int(1)




answer = ''

for i in ans:
    answer += str(int(i))

an=int(answer,2)




ind =100
if an > K:
    cand = np.where(ans==1)[0]
    ind = -1
    
    while an > K:
        lis = list(ans)
        lis[cand[ind]] = 0
        answer = ''
        for i in lis:
            answer += str(int(i))
        lis[cand[ind]] = 1
        an=int(answer,2)
        ind -= 1
        
    ind +=1
    

j = tot
for i in range(len(j)):
    j[i] = max(j[i],N-j[i])
if ind<50:
    j[ind] = N-j[ind]

b = 0
for i in range(len(j)):
    b  += int(j[-i-1])*(2**i)
print(b)

Fixed submission:
T,X = list(map(int, input().split()))
print(T/X)
Buggy python submission (verdict: Runtime Error):
totalcount=0
for i in range(n):
    count=2
    if a[i] == b[i]:
        count=1
        if  b[i]==c[i]:
            count=0
    elif a[i] == c[i]:
        count=1
    elif b[i]==c[i]:
        count=1
    totalcount+=count
print(totalcount)

Fixed submission:
n= int(input())

a=  input()
b = input()
c = input()

totalcount=0
for i in range(n):
    count=2
    if a[i] == b[i]:
        count=1
        if  b[i]==c[i]:
            count=0
    elif a[i] == c[i]:
        count=1
    elif b[i]==c[i]:
        count=1
    totalcount+=count
print(totalcount)
Buggy python submission (verdict: Runtime Error):
n1=input()
n2=input()
n3=input()
out=(n1-n2+1)*(n1-n3+1)
print(out)

Fixed submission:
n1=int(input())
n2=int(input())
n3=int(input())
out=(n1-n2+1)*(n1-n3+1)
print(out)

Buggy python submission (verdict: Runtime Error):
n1, n2, n3 = list(map(int, input().split()))
print((n1-n2+1)*(n1-n3+1))

Fixed submission:
n1=int(input())
n2=int(input())
n3=int(input())
out=(n1-n2+1)*(n1-n3+1)
print(out)

Buggy python submission (verdict: Runtime Error):
n1, n2, n3 = list(map(int, input().split()))
print((n1-n2)*(n1-n3))

Fixed submission:
n1=int(input())
n2=int(input())
n3=int(input())
out=(n1-n2+1)*(n1-n3+1)
print(out)
Buggy python submission (verdict: Runtime Error):
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split()))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
    if i <= A:
       lis_1.append(i)
    elif i > A and i <=B:
       lis_2.append(i)
    else:
       lis_3.append(i)

print(min[len(lis_1),len(lis_2),len(lis_3)])

Fixed submission:
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split()))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
    if i <= A:
       lis_1.append(i)
    elif i > A and i <=B:
       lis_2.append(i)
    else:
       lis_3.append(i)

print(min([len(lis_1),len(lis_2),len(lis_3)]))

Buggy python submission (verdict: Runtime Error):
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split()))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
   if i <= A:
      lis_1.append(i)
   elif i > A and i <=B:
      lis_2.append(i)
   else:
      lis_3.append(i)

print(min[len(lis_1),len(lis_2),len(lis_3)])

Fixed submission:
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split()))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
    if i <= A:
       lis_1.append(i)
    elif i > A and i <=B:
       lis_2.append(i)
    else:
       lis_3.append(i)

print(min([len(lis_1),len(lis_2),len(lis_3)]))

Buggy python submission (verdict: Runtime Error):
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split())))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
   if i <= A:
      lis_1.append(i)
   elif i > A and i <=B:
      lis_2.append(i)
   else:
      lis_3.append(i)

print(min[len(lis_1),len(lis_2),len(lis_3)])

Fixed submission:
N=int(input())
A,B  = list(map(int, input().split()))
lis = list(map(int, input().split()))
lis_1=[]
lis_2=[]
lis_3=[]
for i in lis:
    if i <= A:
       lis_1.append(i)
    elif i > A and i <=B:
       lis_2.append(i)
    else:
       lis_3.append(i)

print(min([len(lis_1),len(lis_2),len(lis_3)]))
Buggy python submission (verdict: Runtime Error):
n = int(input())
lis=[]
for i in range(n):
    lis.append(int(input()))

lis.sort()

count=0
while len(lis)>0:
    if lis[0]==lis[1]:
        del lis[:2]
    else:
        del lis[0]
        count+=1
print(count)

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

lis.sort()

count=0
while len(lis)>1:
    if lis[0]==lis[1]:
        del lis[:2]
    else:
        del lis[0]
        count+=1
print(count+len(lis))
Buggy python submission (verdict: Runtime Error):
s = list(map(int, input()))0
print(min(len(s)-sum(s), sum(s))*2)

Fixed submission:
s = list(map(int, input()))
print(min(len(s)-sum(s), sum(s))*2)
Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

n = int(input())
enemies = list(map(int,input().split()))
print(reduce(math.gcd, enemies))

Fixed submission:
from functools import reduce

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

n = int(input())
enemies = list(map(int,input().split()))
print(reduce(gcd, enemies))

Buggy python submission (verdict: Runtime Error):
import math
from functools import reduce

n = int(input())
enemies = list(map(int,input().split()))
gcd(reduce(math.gcd, enemies))

Fixed submission:
from functools import reduce

def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

n = int(input())
enemies = list(map(int,input().split()))
print(reduce(gcd, enemies))
Buggy python submission (verdict: Runtime Error):
y = x
cnt = 0
for ai in a:
    if y - ai >= 0:
        y -= ai
        cnt += 1
if y and cnt == n:
    print(cnt - 1)
else:
    print(cnt)

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

y = x
cnt = 0
for ai in a:
    if y - ai >= 0:
        y -= ai
        cnt += 1
if y and cnt == n:
    print(cnt - 1)
else:
    print(cnt)
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left
N, S = map(int, input().split())
x = list(map(int, input().split()))
x.sort()
i = bisect_left(x, S)
if x[i] != S:
    x.insert(i, S)
    N += 1
diff = []
for i in range(N - 1):
    diff.append(x[i + 1] - x[i])

# 最小公倍数を求める
def lcm(a, b):
    while b != 0:
        a, b = b, a % b
    return a
ans = diff[0]
for d in diff[1:]:
    ans = lcm(ans, d)
print(ans)

Fixed submission:
N, S = map(int, input().split())
x = list(map(int, input().split()))
x.append(S)
x.sort()
diff = []
for i in range(N):
    diff.append(x[i + 1] - x[i])

# 最小公倍数を求める
def lcm(a, b):
    while b != 0:
        a, b = b, a % b
    return a

ans = diff[0]
for d in diff[1:]:
    ans = lcm(ans, d)
print(ans)
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
cake = [list(map(int, input().split())) for _ in range(N)]

INF = 10 ** 18
dp = [[[-INF] * 8 for _ in range(M + 1)] for _ in range(N + 1)]
for i in range(8):
    dp[0][0][i] = 0

for i in range(1, N + 1):
    x, y, z = cake[i - 1]
    for j in range(1, i + 1):
        for msk in range(8):
            mx = x * (1 - 2 * (msk >> 2 & 1))
            my = y * (1 - 2 * (msk >> 1 & 1))
            mz = z * (1 - 2 * (msk >> 0 & 1))
            dp[i][j][msk] = max(dp[i - 1][j][msk],
                                dp[i - 1][j - 1][msk] + mx + my + mz)

print(max(dp[N][M][i] for i in range(8)))

Fixed submission:
N, M = map(int, input().split())
cake = [list(map(int, input().split())) for _ in range(N)]

INF = 10 ** 18
dp = [[[-INF] * 8 for _ in range(M + 1)] for _ in range(N + 1)]
for i in range(N + 1):
    for msk in range(8):
        dp[i][0][msk] = 0

for i in range(1, N + 1):
    x, y, z = cake[i - 1]
    for j in range(1, M + 1):
        for msk in range(8):
            mx = x * (1 - 2 * (msk >> 2 & 1))
            my = y * (1 - 2 * (msk >> 1 & 1))
            mz = z * (1 - 2 * (msk >> 0 & 1))
            dp[i][j][msk] = max(dp[i - 1][j][msk],
                                dp[i - 1][j - 1][msk] + mx + my + mz)

print(max(dp[N][M][i] for i in range(8)))

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

dp = [[[0] * 8 for _ in range(M + 1)] for _ in range(N + 1)]

for i in range(1, N + 1):
    x, y, z = cake[i - 1]
    for j in range(1, M + 1):
        for msk in range(8):
            mx = x * (1 - 2 * (msk >> 2 & 1))
            my = y * (1 - 2 * (msk >> 1 & 1))
            mz = z * (1 - 2 * (msk >> 0 & 1))
            dp[i][j][msk] = max(dp[i - 1][j][msk],
                                dp[i - 1][j - 1][msk] + mx + my + mz)

print(max(dp[N][M][i] for i in range(8)))

Fixed submission:
N, M = map(int, input().split())
cake = [list(map(int, input().split())) for _ in range(N)]

INF = 10 ** 18
dp = [[[-INF] * 8 for _ in range(M + 1)] for _ in range(N + 1)]
for i in range(N + 1):
    for msk in range(8):
        dp[i][0][msk] = 0

for i in range(1, N + 1):
    x, y, z = cake[i - 1]
    for j in range(1, M + 1):
        for msk in range(8):
            mx = x * (1 - 2 * (msk >> 2 & 1))
            my = y * (1 - 2 * (msk >> 1 & 1))
            mz = z * (1 - 2 * (msk >> 0 & 1))
            dp[i][j][msk] = max(dp[i - 1][j][msk],
                                dp[i - 1][j - 1][msk] + mx + my + mz)

print(max(dp[N][M][i] for i in range(8)))
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import product, permutations
n, c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]
grid = [list(map(int, input().split())) for _ in range(n)]
cost = [[0] * c for _ in range(3)]

for i, j, k in product(range(n), range(n), range(c)):
    # cost[0][k] -> 余りが0になる場所の色をkに変えるときのコスト
    cost[(i + j + 2) % 3][k] += d[grid[i][j] - 1][k]

ans = []
for s, t, u in permutations(range(c), r=3):
    ans.append(cost[0][s] + cost[1][t] + cost[2][u])
print(min(ans))

Fixed submission:
#%% D
from itertools import product, permutations
n, c = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(c)]
C = [list(map(int, input().split())) for _ in range(n)]


X = [[0] * c for i in range(3)]

for i in range(n):
    for j in range(n):
        X[(i + j + 2) % 3][C[i][j] - 1] += 1

cost = [[0] * c for i in range(3)]

for i in range(c):
    for j in range(c):
        cost[0][j] += X[0][i] * D[i][j]
        cost[1][j] += X[1][i] * D[i][j]
        cost[2][j] += X[2][i] * D[i][j]

ans = []
for s, t, u in permutations(range(c), r=3):
    ans.append(cost[0][s] + cost[1][t] + cost[2][u])
print(min(ans))
Buggy python submission (verdict: Runtime Error):
N = int(input())
S = input()
sum_s = [0] * N
# [0, i) のWの数
for i in range(1, N + 1):
    if S[i - 1] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]
        
ans = sum_s[N + 1]
for i in range(0, N):
    lc = sum_s[i]
    rc = sum_s[N + 1] - sum_s[i + 1] 
    min(ans, lc + rc)

print(ans)

Fixed submission:
N = int(input())
S = input()
sum_s = [0] * N
sum_s[0] = 1 if S[0] == 'W' else 0
for i in range(1, N):
    if S[i] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]

lc = 0
rc = (N - 1) - (sum_s[N - 1] - sum_s[0])
ans = lc + rc

for i in range(1, N):
    lc = sum_s[i - 1]
    rc = (N - (i + 1)) - (sum_s[N - 1] - sum_s[i])

    ans = min(ans, lc + rc)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = [input() for _ in range(N)]
ans = 0
for k in range(N):
    f = True
    for i in range(N):
        for j in range(i + 1, N):
            if S[i][(j + k) % N] != S[j][(i + k) % N]:
                f = False
                break

        if not f:
            break
    if f:
        ans += N
print(ans)

Fixed submission:
N = int(input())
S = [input() for _ in range(N)]
ans = 0
for k in range(N):
    f = True
    for i in range(N):
        for j in range(i + 1, N):
            if S[i][(j + k) % N] != S[j][(i + k) % N]:
                f = False
                break

        if not f:
            break
    if f:
        ans += N
print(ans)
Buggy python submission (verdict: Runtime Error):
print((x - a) % b)

Fixed submission:
x, a, b = [int(input()) for _ in range(3)]
print((x - a) % b)
Buggy python submission (verdict: Runtime Error):
s = input()
x, y = map(int, input().split())
m = list(map(len, s.split('T')))
# 最初は右に行くしかない
dp_x = {m[0]}
for i in m[2::2]:
    new_dp = set()
    for v in dp_x:
        new_dp += {v + i, v - i}
    dp_x = new_dp

dp_y = {0}
for i in m[1::2]:
    new_dp = set()
    for v in dp_y:
        new_dp += {v + i, v - i}
    dp_y = new_dp

print('Yes' if x in dp_x and y in dp_y else 'No')

print(dp_x)
print(dp_y)

Fixed submission:
s = input()
x, y = map(int, input().split())
m = list(map(len, s.split('T')))
# 最初は右に行くしかない
dp_x = {m[0]}
for i in m[2::2]:
    new_dp = set()
    for v in dp_x:
        new_dp |= {v + i, v - i}
    dp_x = new_dp

dp_y = {0}
for i in m[1::2]:
    new_dp = set()
    for v in dp_y:
        new_dp |= {v + i, v - i}
    dp_y = new_dp

print('Yes' if x in dp_x and y in dp_y else 'No')