TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
import numpy as np

N = int(input())
dp = [float("inf")]*(N + 1)
dp[0] = 0
pull_list_9 = [9**i for i in range(1, 6)]
pull_list_6 = [6**i for i in range(1, 7)]
pull_list = np.array(pull_list_9 + pull_list_6 + [1])
p_s = sorted(pull_list)
for i in range(1, N + 1):
  for j in p_s:
    if i - j < 0:
      break
      dp[i] = min(dp[i], dp[i-j] + 1)
print(dp[-1])


N = int(input())
dp = [float("inf")]*(N + 1)
dp[0] = 0
pull_list_9 = [9**i for i in range(1, 6)]
pull_list_6 = [6**i for i in range(1, 7)]
pull_list = pull_list_9 + pull_list_6 + [1]
p_s = sorted(pull_list)

for i in range(1, N + 1):
  for j in p_s:
    if i - j < 0:
      break
    dp[i] = min(dp[i], dp[i-j] + 1)

print(dp[-1])


Verdicts: Wrong Answer

Accepted
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
import math
N = int(input())

ans = 0
while N>0:
  if math.floor(math.log(N,9)) > 0 or math.floor(math.log(N,6)) > 0:
    print(max(9**(math.floor(math.log(N,9))), 6**(math.floor(math.log(N,6)))))
    N -= max(9**(math.floor(math.log(N,9))), 6**(math.floor(math.log(N,6))))
  else:
    N -= 1
  ans += 1

print(ans)


import math
N = int(input())

ans = N
for i in range(N+1):
  count = 0
  t=i
  while t>0:
    count+=t%6
    t=math.floor(t/6)

  t = N-i

  while t>0:
    count += t%9
    t=math.floor(t/9)

  if ans>count:
    ans = count

print(ans)


Verdicts: Wrong Answer

Accepted
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
import math
n=int(input())
dp=[100000 for i in range(n+1)]
dp[0]=0
dp[1]=1
for i in range(1,n+1):
  dp[i]=dp[i-1]+1
  j,k=int(math.log(i)/math.log(6)),int(math.log(i)/math.log(9))
  for jj in range(j+2):
    dp[i]=min(dp[i],dp[max(0,i-6**jj)]+1)
  for kk in range(k+1):
    dp[i]=min(dp[i],dp[i-9**kk]+1) 
print(dp[n])

n=int(input())
dp=[100000 for i in range(n+1)]
dp[0]=0
dp[1]=1
for i in range(1,n+1):
  dp[i]=dp[i-1]+1
  j,k=0,0
  while 6**j<=i:
    dp[i]=min(dp[i],dp[i-6**j]+1)
    j+=1
  while 9**k<=i:
    dp[i]=min(dp[i],dp[i-9**k]+1)
    k+=1
print(dp[n])


Verdicts: Wrong Answer

Accepted
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
N = int(input())
dp = [0] * (N + 1)
dp[0] = 0
print(dp)
for i in range(1, N + 1):
    cnt = i
    for j in range(1, 7):
        q = 6 ** j
        if i >= q:
            cnt = min(cnt, dp[i - q] + 1)
            print(i,cnt)
    for j in range(1, 6):
        q = 9 ** j
        if i >= q:
            cnt = min(cnt, dp[i - q] + 1)
    dp[i] = cnt
print(dp)


N = int(input())
dp = [0] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    cnt = i
    for j in range(1, 7):
        q = 6 ** j
        if i >= q:
            cnt = min(cnt, dp[i - q] + 1)
    for j in range(1, 6):
        q = 9 ** j
        if i >= q:
            cnt = min(cnt, dp[i - q] + 1)
    dp[i] = cnt
print(dp[N])

Verdicts: Wrong Answer

Accepted
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
N = int(input())
print(1)

N = int(input())

memo = [N+1] * (N+1)

memo[0] = 0
memo[1] = 1

exp_sixs = []
exp_nines = []

for i in range(1, 100000):
    if 6 ** i <= N:
        exp_sixs.append(6**i)
    else:
        break

for i in range(1, 100000):
    if 9 ** i <= N:
        exp_nines.append(9**i)
    else:
        break

# print(exp_sixs)
# print(exp_nines)
for exp_six  in exp_sixs:
    memo[exp_six] = 1

for exp_nine in exp_nines:
    memo[exp_nine] = 1

for i in range(1, N+1):
    
    one_result = memo[i-1] + 1

    
    six_result = N+1
    for exp_six in exp_sixs:
        if (i - exp_six) >= 0:
            six_result = min(six_result, memo[i-exp_six] + 1)
        else:
            break

    
    nine_result = N+1
    for exp_nine in exp_nines:
        if (i - exp_nine) >= 0:
            nine_result = min(nine_result, memo[i-exp_nine] + 1)
        else:
            break
    memo[i] = min(one_result, six_result, nine_result)
print(memo[N])


Verdicts: Wrong Answer

Accepted
Problem: To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    * 1 yen (the currency of Japan)    * 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    * 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...  At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.

Submission attempts (in order):
n=int(input())

six=[6**i for i in range(1, 7)]
nine=[9**i for i in range(0, 6)]
Cash=sorted(six+nine, reverse=True)

ans=0
i=0
while i<len(Cash):
    c=Cash[i]
    num=n//c
    n-=num*c
    ans+=num
    print(num, c, n)
    if n==0:
        break
    i+=1
print(ans)
    


n=int(input())

dp=[200000]*(n+1)
dp[0]=0

for i in range(n+1):
    power=1
    while power<=i:
        dp[i]=min(dp[i], dp[i-power]+1)
        power*=6
    power=9
    while power<=i:
        dp[i]=min(dp[i], dp[i-power]+1)
        power*=9

print(dp[n])

Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
N,C = map(int, input().split())
cost = [list(map(int, input().split())) for _ in range(C)]
color = [list(map(int, input().split())) for _ in range(N)]

mod3 = {0:[], 1:[], 2:[]}
for i in range(N):
    for j in range(N):
        m = (i+j)%3
        mod3[m].append(color[i][j])
        
from collections import Counter
from itertools import combinations, permutations
cnts = [Counter(mod3[i]) for i in range(3)]
# print(cnts)
ans = float('inf')
for comb in combinations(range(C), 3):
    for p in permutations(comb):
        cond = 0
        for i in range(3):
            for col in cnts[i].keys():
                cond += cnts[i][col]*cost[col][p[i]]
        ans = min(ans, cond)
        #print(cond, p)
print(ans)
    

from collections import Counter
from itertools import permutations
N,C = map(int, input().split())
cost = [list(map(int, input().split())) for _ in range(C)]
color = [list(map(lambda x:int(x)-1, input().split())) for _ in range(N)]

# 3 groups
mod3 = {0:[], 1:[], 2:[]}
for i in range(N):
    for j in range(N):
        m = (i+j)%3
        mod3[m].append(color[i][j])

cnts = [Counter(mod3[i]) for i in range(3)]
cost_mod = [[-1]*C for _ in range(3)]
for i in range(3):
    for to_ in range(C):
        cond = 0
        for from_ in cnts[i].keys():
            cond += cnts[i][from_]*cost[from_][to_]
        cost_mod[i][to_] = cond

ans = float('inf')
for p in permutations(range(C), 3):
    cond = 0
    for i in range(3):
        cond += cost_mod[i][p[i]]
    ans = min(ans, cond)
    #print(cond, p)
print(ans)
    

Verdicts: Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
import sys
s =sys.stdin.readlines()
N,C = map(int,s[0].split())
D = [[int(i) for i in sys[1+j].split()] for j in range(C)]
c = [[int(i) for i in sys[C+j].split()] for j in range(N)]

D_sum = [[0]*C for _ in range(3)]

for i in range(C):
    for j in range(N):
        for k in range(N):
            D_sum[(j+k)%3][i] += D[c[j][k]-1][i]
            
ans = 1000*500*500
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i==j or j==k or k==i:
                continue
            else:
                ans = min(ans, D_sum[0][i]+D_sum[1][j]+D_sum[2][k])
print(ans)      


import sys

s = sys.stdin.readlines()

N,C = map(int,s[0].split())
D = [[int(i) for i in s[j+1].split()] for j in range(C)]
c = [[int(i) for i in s[c+1+j].split()] for j in range(N)]

c_sum = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        c_sum[(i+j)%3][c[i][j]-1] += 1

D_sum = [[0]*C for _ in range(3)]
for i in range(C):
    for j in range(C):
        for k in range(3):
            D_sum[k][j] += D[i][j] * c_sum[k][i]

ans = 1000*500*500
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i==j or j==k or k==i:
                continue
            else:
                ans = min(ans, D_sum[0][i]+D_sum[1][j]+D_sum[2][k])
print(ans)


N,C = map(int,input().split())
D = [[int(i) for i in input().split()] for j in range(C)]
c = [[int(i) for i in input().split()] for j in range(N)]

c_sum = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        c_sum[(i+j)%3][c[i][j]-1] += 1

D_sum = [[0]*C for _ in range(3)]
for i in range(C):
    for j in range(C):
        for k in range(3):
            D_sum[k][j] += D[i][j] * c_sum[k][i]

ans = 1000*500*500
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i==j or j==k or k==i:
                continue
            else:
                ans = min(ans, D_sum[0][i]+D_sum[1][j]+D_sum[2][k])
print(ans)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
N,C = [int(i) for i in input().split()]
D = [[int(j) for j in input().split()] for i in range(C)]
c_temp = [[int(j)-1 for j in input().split()] for i in range(N)]
c = [[0 for i in range(C)] for j in range(3)]
print(c)
for i in range(N):
    for j in range(N):
        c[(i+j+2)%3][c_temp[i][j]] += 1
        print((i+j+2)%3, c_temp[i][j])

min = 100000
for i in [ii for ii in range(C)]:
    for j in [jj for jj in range(C) if jj != i]:
        for k in [kk for kk in range(C) if kk != i and kk != j]:
            count = sum([D[x][i]*c[0][x] + D[x][j]*c[1][x] + D[x][k]*c[2][x] for x in range(C)])
            if min > count:
                min = count
print(min)




N,C = [int(i) for i in input().split()]
D = [[int(j) for j in input().split()] for i in range(C)]
c_temp = [[int(j)-1 for j in input().split()] for i in range(N)]
c = [[0 for i in range(C)] for j in range(3)]
print(c)
for i in range(N):
    for j in range(N):
        c[(i+j+2)%3][c_temp[i][j]] += 1

min = 100000
for i in [ii for ii in range(C)]:
    for j in [jj for jj in range(C) if jj != i]:
        for k in [kk for kk in range(C) if kk != i and kk != j]:
            count = sum([D[x][i]*c[0][x] + D[x][j]*c[1][x] + D[x][k]*c[2][x] for x in range(C)])
            if min > count:
                min = count
print(min)




N,C = [int(i) for i in input().split()]
D = [[int(j) for j in input().split()] for i in range(C)]
c_temp = [[int(j)-1 for j in input().split()] for i in range(N)]
c = [[0 for i in range(C)] for j in range(3)]
for i in range(N):
    for j in range(N):
        c[(i+j)%3][c_temp[i][j]] += 1

min = 10**15
for i in [ii for ii in range(C)]:
    for j in [jj for jj in range(C) if jj != i]:
        for k in [kk for kk in range(C) if kk != i and kk != j]:
            count = sum([D[x][i]*c[0][x] + D[x][j]*c[1][x] + D[x][k]*c[2][x] for x in range(C)])
            if min > count:
                min = count
print(min)




Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
from collections import Counter
from itertools import permutations

def main():
    N, C = map(int, input().split())
    D = [[int(x) for x in input().split()] for _ in range(C)]

    cnt = [Counter() for _ in range(3)]

    for i in range(N):
        c = [int(x)-1 for x in input().split()]
        for j in range(3):
            cnt[j].update(c[(3-(i+2)+j)%3::3])

    ans = 1000*500*500+5
    for p in permutations(range(C), 3):
        s = sum(D[k][p[j]] * v for k, v in cnt[j].items() for j in range(3))
        ans = min(ans, s)

    print(ans)

main()

from collections import Counter
from itertools import permutations

def main():
    N, C = map(int, input().split())
    D = tuple(tuple(map(int, input().split())) for _ in range(C))

    cnt = [Counter() for _ in range(3)]

    for i in range(N):
        c = [int(x)-1 for x in input().split()]
        for j in range(3):
            cnt[j].update(c[(3-(i+2)+j)%3::3])

    ans = 1000*500*500+5
    for p in permutations(range(C), 3):
        s = 0
        for j in range(3):
            s += sum(D[k][p[j]] * v for k, v in cnt[j].items())
        if s < ans: ans = s
    print(ans)

main()


Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
import itertools
N,C = map(int,input().split(' '))
D = [list(map(int,input().split(' '))) for i in [0]*C]
c = [list(map(int,input().split(' '))) for i in [0]*N]


l = [[],[],[]]
n = 0
for i in range(N):
	for j in range(i+1):
		l[n].append(c[i-j][j])
	n = (n+1) % 3
for i in range(N-1):
	for j in range(N-i-1):
		l[n].append(c[i+j+1][N-j-1])
	n = (n+1) % 3

d1,d2,d3 = [[sum([D[j-1][i] for j in l[n]]) for i in range(C)] for n in range(3)]

ans = 1000*500*500
colors = list(range(C))
for c1,c2,c3 in itertools.combinations(colors,3):
	ans = min(ans,d1[c1] + d2[c2] + d3[c3])
print(ans)







import itertools
N,C = map(int,input().split(' '))
D = [list(map(int,input().split(' '))) for i in [0]*C]
c = [list(map(int,input().split(' '))) for i in [0]*N]


l = [[],[],[]]
n = 0
for i in range(N):
	for j in range(i+1):
		l[n].append(c[i-j][j])
	n = (n+1) % 3
for i in range(N-1):
	for j in range(N-i-1):
		l[n].append(c[i+j+1][N-j-1])
	n = (n+1) % 3

d1,d2,d3 = [[sum([D[j-1][i] for j in l[n]]) for i in range(C)] for n in range(3)]

ans = 1000*500*500
colors = list(range(C))
for c1,c2,c3 in itertools.permutations(colors,3):
	ans = min(ans,d1[c1] + d2[c2] + d3[c3])
print(ans)





Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
#!/usr/bin/env python3

from itertools import product

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

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


colors = [[0]*C for i in range(3)]
for i in range(N):
    tmp = list(map(int, input().split()))
    for j, color in enumerate(tmp):
        num = ((i+1) + (j+1)) % 3
        colors[num][color-1] += 1


data = [[0]*C for i in range(3)]


for i in range(3):
    # color[i]
    for j in range(C):
        for k in range(C):
            data[i][k] += colors[i][j] * D[j][k]
# print(data)

ans = 10**10
# for iter_ in product([0, 1, 2], repeat=C):
#     if len(set(iter_)) != C:
#         continue
#     tmp = []
#     for i, j in enumerate(iter_):
#         tmp.append(data[i][j])
#     ans = min(ans, sum(tmp))
print(ans)


#!/usr/bin/env python3

from itertools import permutations

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

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


colors = [[0]*C for i in range(3)]
for i in range(N):
    tmp = list(map(int, input().split()))
    for j, color in enumerate(tmp):
        num = ((i+1) + (j+1)) % 3
        colors[num][color-1] += 1


data = [[0]*C for i in range(3)]


for i in range(3):
    # color[i]
    for j in range(C):
        for k in range(C):
            data[i][k] += colors[i][j] * D[j][k]
# print(data)

ans = 10**10
for iter_ in permutations(list(range(C)), 3):
    # print(iter_)
    tmp = []
    for i, j in enumerate(iter_):
        tmp.append(data[i][j])
    ans = min(ans, sum(tmp))
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
n,c=map(int,input().split())
D=[]
for i in range(c):
    D.append(list(map(int,input().split())))
C=[]
for i in range(n):
    C.append(list(map(int,input().split())))

print(D)

C1=[]
C2=[]
C3=[]
for i in range(n):
    for j in range(n):
        f=(i+j)%3
        if f==0:
            C1.append(C[i][j])
        if f==1:
            C2.append(C[i][j])
        if f==2:
            C3.append(C[i][j])
        

miniwa=1000*500*500

print(C3)

for c1 in range(0,c):
    iwa1=sum(D[i-1][c1] for i in C1)
    for c2 in range(0,c):
        iwa2=sum(D[j-1][c2] for j in C2)
        for c3 in range(0,c):
            if c1!=c2 and c2!=c3 and c3!=c1 :
                iwa3=sum(D[k-1][c3] for k in C3)
                iwa=iwa1+iwa2+iwa3
                miniwa=min(miniwa,iwa)

print(miniwa)


n,c=map(int,input().split())
D=[]
for i in range(c):
    D.append(list(map(int,input().split())))
C=[]
for i in range(n):
    C.append(list(map(int,input().split())))

#print(D)

C1=[]
C2=[]
C3=[]
for i in range(n):
    for j in range(n):
        f=(i+j)%3
        if f==0:
            C1.append(C[i][j])
        if f==1:
            C2.append(C[i][j])
        if f==2:
            C3.append(C[i][j])
        

miniwa=1000*500*500

#print(C3)
iwa1=[]
iwa2=[]
iwa3=[]

for cc in range(0,c):
    iwa1.append(sum(D[i-1][cc] for i in C1))
    iwa2.append(sum(D[i-1][cc] for i in C2))
    iwa3.append(sum(D[i-1][cc] for i in C3))

for c1 in range(0,c):
    for c2 in range(0,c):
        for c3 in range(0,c):
            if c1!=c2 and c2!=c3 and c3!=c1 :
                iwa=iwa1[c1]+iwa2[c2]+iwa3[c3]
                miniwa=min(miniwa,iwa)

print(miniwa)


Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
from collections import defaultdict

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)]

mod_0=defaultdict(int)
mod_1=defaultdict(int)
mod_2=defaultdict(int)

for i in range(N):
    for j in range(N):
        n=(i+j+2)%3
        if n==0:
            mod_0[grid[i][j]]+=1
        elif n==1:
            mod_1[grid[i][j]]+=1
        else:
            mod_2[grid[i][j]]+=1

ans=float("inf")
for i in range(C):
    for j in range(C):
        if i==j:
            continue
        for k in range(C):
            if k in [i,j]:
                continue

            total
            for prev,cnt in mod_0.items():
                total+=D[prev-1][i]*cnt
            for prev,cnt in mod_1.items():
                total+=D[prev-1][j]*cnt
            for prev,cnt in mod_2.items():
                total+=D[prev-1][k]*cnt
            
            ans=min(ans,total)
print(ans)

from collections import defaultdict

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)]

mod_0=defaultdict(int)
mod_1=defaultdict(int)
mod_2=defaultdict(int)

for i in range(N):
    for j in range(N):
        n=(i+j+2)%3
        if n==0:
            mod_0[grid[i][j]]+=1
        elif n==1:
            mod_1[grid[i][j]]+=1
        else:
            mod_2[grid[i][j]]+=1

ans=float("inf")
for i in range(C):
    for j in range(C):
        if i==j:
            continue
        for k in range(C):
            if k in [i,j]:
                continue

            total=0
            for prev,cnt in mod_0.items():
                total+=D[prev-1][i]*cnt
            for prev,cnt in mod_1.items():
                total+=D[prev-1][j]*cnt
            for prev,cnt in mod_2.items():
                total+=D[prev-1][k]*cnt
            
            ans=min(ans,total)
print(ans)

Verdicts: Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
N,C=map(int,input().split())
D=[list(map(int,input().split())) for _ in range(C)]
G=[list(map(int,input().split())) for _ in range(N)]

miniwa=250000000
for c0 in range(1,C+1):
    for c1 in range(1,C+1):
        for c2 in range(1,C+1):
            if (c1-c0)*(c2-c1)*(c0-c2)==0: break
            iwa=0
            for x in range(N):
                for y in range(N):
                    if (x+y+2)%3==0: iwa+=D[G[x][y]-1][c0-1]
                    elif (x+y+2)%3==1: iwa+=D[G[x][y]-1][c1-1]
                    elif (x+y+2)%3==2: iwa+=D[G[x][y]-1][c2-1]
            miniwa=min(miniwa,iwa)
print(miniwa)

N,C=map(int,input().split())
D=[list(map(int,input().split())) for _ in range(C)]
G=[list(map(int,input().split())) for _ in range(N)]
C0=[0]*(C+1)
C1=[0]*(C+1)
C2=[0]*(C+1)

for x in range(N):
    for y in range(N):
        if (x+y+2)%3==0: C0[G[x][y]]+=1
        elif (x+y+2)%3==1: C1[G[x][y]]+=1
        elif (x+y+2)%3==2: C2[G[x][y]]+=1

miniwa=250000000
for c0 in range(1,C+1):
    for c1 in range(1,C+1):
        for c2 in range(1,C+1):
            if (c1-c0)*(c2-c1)*(c0-c2)==0: break
            iwa=0
            for i in range(1,C+1):
                iwa+=(D[i-1][c0-1]*C0[i]+D[i-1][c1-1]*C1[i]+D[i-1][c2-1]*C2[i])
            miniwa=min(miniwa,iwa)
print(miniwa)

N,C=map(int,input().split())
D=[list(map(int,input().split())) for _ in range(C)]
G=[list(map(int,input().split())) for _ in range(N)]
C0=[0]*(C+1)
C1=[0]*(C+1)
C2=[0]*(C+1)

for x in range(N):
    for y in range(N):
        if (x+y+2)%3==0: C0[G[x][y]]+=1
        elif (x+y+2)%3==1: C1[G[x][y]]+=1
        elif (x+y+2)%3==2: C2[G[x][y]]+=1

miniwa=250000000
for c0 in range(1,C+1):
    for c1 in range(1,C+1):
        for c2 in range(1,C+1):
            if (c1-c0)*(c2-c1)*(c0-c2)==0: continue
            iwa=0
            for i in range(1,C+1):
                iwa+=(D[i-1][c0-1]*C0[i]+D[i-1][c1-1]*C1[i]+D[i-1][c2-1]*C2[i])
            miniwa=min(miniwa,iwa)
print(miniwa)

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
import functools  # Python3 
N,M = map(int,input().split())
x, y, z = [0] * N, [0] * N, [0] * N
for i in range(N):
	x[i], y[i], z[i] = map(int,input().split())

if M==0:
	print(0)
	exit()

ppp = functools.reduce(lambda x,y: x+y,sorted([ x[i]+y[i]+z[i] for i in range(N)])[-M:])
pmp = functools.reduce(lambda x,y: x+y,sorted([ x[i]-y[i]+z[i] for i in range(N)])[-M:])
ppm = functools.reduce(lambda x,y: x+y,sorted([ x[i]+y[i]-z[i] for i in range(N)])[-M:])
pmm = functools.reduce(lambda x,y: x+y,sorted([ x[i]-y[i]-z[i] for i in range(N)])[-M:])
mpp = functools.reduce(lambda x,y: x+y,sorted([-x[i]+y[i]+z[i] for i in range(N)])[-M:])
mpm = functools.reduce(lambda x,y: x+y,sorted([-x[i]+y[i]-z[i] for i in range(N)])[-M:])
mmp = functools.reduce(lambda x,y: x+y,sorted([-x[i]-y[i]+z[i] for i in range(N)])[-M:])
mmm = functools.reduce(lambda x,y: x+y,sorted([-x[i]-y[i]-z[i] for i in range(N)])[-M:])

MAX = 0
for lis in [ppp,pmp,ppm,pmm,mpp,mpm,mmp,mmm]:
	if lis  > MAX:
		MAX = lis
print(MAX)


N,C = map(int,input().split())
D = []
for i in range(C):
    D.append(list(map(int,input().split())))
c = []
for i in range(N):
    c.append(list(map(int,input().split())))

import itertools
current_minimum = 1000 * 500 * 500 + 1
Z = []
Z.append([0]*C)
Z.append([0]*C)
Z.append([0]*C)
#print(Z)

for (i,j) in itertools.product(range(N),range(N)):
	cur_mod = ((i+1)+(j+1)) % 3
	cur_color = c[i][j]
	#print(cur_mod)
	#print(cur_color)
	Z[cur_mod][cur_color-1] += 1

current_minimum = 1000 * 500 * 500 + 1
for (c0,c1,c2) in itertools.permutations(range(C), 3):
	tmp = 0
	color = [c0,c1,c2]
	for cur_mod in range(3):
		target_color = color[cur_mod]
		for cur_color in range(C):
			tmp += Z[cur_mod][cur_color] * D[cur_color][target_color]
	if current_minimum >= tmp:
		current_minimum = tmp
print(current_minimum)


Verdicts: Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
from collections import defaultdict
tot = 10**12
n,c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(n)]
clr = [list(map(int, input().split())) for _ in range(n)]
l = [dict()]*3
for y in range(n):
  for x in range(n):
    l[(x+y)%3][clr[y][x]] += 1
for c1 in range(c):
  for c2 in range(c):
    if c1 == c2:
      continue
    for c3 in range(c):
      if c1==c3 or c2==c3:
        continue
      ctot = 0
      cl = [c1,c2,c3]
      for i in range(3):
        for x in l[i]:
          ctot += d[x-1][c[i]]*l[i][x]
      if tot > ctot:
        tot = ctot
print(tot)
      

from collections import defaultdict as ddict
tot = 10**12
n,c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]
clr = [list(map(int, input().split())) for _ in range(n)]
l = [ddict(int) for _ in range(3)]
for y in range(n):
  for x in range(n):
    l[(x+y)%3][clr[y][x]] += 1
for c1 in range(c):
  for c2 in range(c):
    if c1 == c2:
      continue
    for c3 in range(c):
      if c1==c3 or c2==c3:
        continue
      ctot = 0
      cl = [c1,c2,c3]
      for i in range(3):
        for x in l[i]:
          ctot += d[x-1][cl[i]]*l[i][x]
      if tot > ctot:
        tot = ctot
print(tot)

Verdicts: Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
n,c=map(int,input().split())
d=[list(map(int,input().split()))for _ in range(c)]
l=[[i for i in[0]*c]for _ in range(3)]
for i in range(n):
  t=list(map(int,input().split()))
  for j in range(n):
    l[(i+j)%3][t[j]-1]+=1
c=10**18
for i in range(c):
  for j in range(c):
    for k in range(c):
      if i==j or j==k or k==i:continue
      c=min(c,sum(d[x][i]*l[0][x]+d[x][j]*l[1][x]+d[x][k]*l[2][x]for x in range(c)))
print(c)

n,c=map(int,input().split());d=[list(map(int,input().split()))for _ in range(c)];l=[[i for i in[0]*c]for _ in range(3)]
for i in range(n):
  for j,t in zip(range(n),map(int,input().split())):l[(i+j)%3][t-1]+=1
print(min([[sum(d[x][i]*l[0][x]+d[x][j]*l[1][x]+d[x][k]*l[2][x]for x in range(c))for k in range(c)if i!=j and j!=k and k!=i]for j in range(c)]for i in range(c)))

n,c=map(int,input().split())
d=[list(map(int,input().split()))for _ in range(c)]
l=[[i for i in[0]*c]for _ in range(3)]
for i in range(n):
  t=list(map(int,input().split()))
  for j in range(n):
    l[(i+j)%3][t[j]-1]+=1
a=10**18
for i in range(c):
  for j in range(c):
    for k in range(c):
      if i==j or j==k or k==i:continue
      a=min(a,sum(d[x][i]*l[0][x]+d[x][j]*l[1][x]+d[x][k]*l[2][x]for x in range(c)))
print(a)

Verdicts: Time Limit Exceeded

Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
N, C = map(int, input().split())
D = []
for i in range(C):
    D.append(list(map(int, input().split())))

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


c_arr1 = []
c_arr2 = []
c_arr3 = []

for i in range(N):
    for j in range(N):
        if (i+j)%3 == 0: 
            c_arr1.append(c_arr[i][j])
        if (i+j)%3 == 1: 
            c_arr2.append(c_arr[i][j])
        if (i+j)%3 == 2: 
            c_arr3.append(c_arr[i][j])

tmp = 1000*500*500

def cost(arr, aft):
    sum = 0
    for i in range(len(arr)):
        sum += D[arr[i]-1][aft]
    return sum

ans_arr = []

for l in range(C):
    for m in range(C):
        if l == m:
            pass
        else:
            for n in range(C):
                if l == n or m == n:
                    pass
                else:
                    print(l,m,n)
                    ans_arr.append(cost(c_arr1, l) + cost(c_arr2, m) + cost(c_arr3, n))

print(min(ans_arr))

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

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


c_arr1 = []
c_arr2 = []
c_arr3 = []

for i in range(N):
    for j in range(N):
        if (i+j)%3 == 0: 
            c_arr1.append(c_arr[i][j])
        if (i+j)%3 == 1: 
            c_arr2.append(c_arr[i][j])
        if (i+j)%3 == 2: 
            c_arr3.append(c_arr[i][j])

tmp = 1000*500*500

def cost(arr, aft):
    sum = 0
    for i in range(len(arr)):
        sum += D[arr[i]-1][aft]
    return sum

ans_arr1 = []
ans_arr2 = []
ans_arr3 = []

for l in range(C):
    ans_arr1.append(cost(c_arr1, l))
for l in range(C):
    ans_arr2.append(cost(c_arr2, l))
for l in range(C):
    ans_arr3.append(cost(c_arr3, l))


ans_arr = []
for l in range(C):
    for m in range(C):
        for n in range(C):
            if l == n or n == m or m == l:
                pass
            else:
                ans_arr.append(ans_arr1[l]+ans_arr2[m]+ans_arr3[n])

print(min(ans_arr))

Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
import numpy as np


n, c = map(int, input().split())

a = []
b = []

for _ in range(c):
    array = list(map(int, input().split()))
    a.append(array)

for _ in range(n):
    array = list(map(int, input().split()))
    b.append(array)

d = np.array(b)
d = d.flatten()

d1 = d[0::3]
d2 = d[1::3]
d3 = d[2::3]

ans_1 = []
ans_2 = []
ans_3 = []

for i in range(c):
    sum = 0
    for j in d1:
        sum += a[j-1][i]
    ans_1.append(sum)

for i in range(c):
    sum = 0
    for j in d2:
        sum += a[j-1][i]
    ans_2.append(sum)

for i in range(c):
    sum = 0
    for j in d3:
        sum += a[j-1][i]
    ans_3.append(sum)

min_ans = 10**10

import itertools

r = list(range(c))

for i, j, k in itertools.permutations(r, 3):
    ans = ans_1[i] + ans_2[j] + ans_3[k]
    if ans < min_ans:
        min_ans = ans


print(min_ans)


from collections import Counter
from itertools import permutations

n, c = map(int, input().split())

a = []
b = []

for _ in range(c):
    array = list(map(int, input().split()))
    a.append(array)

for _ in range(n):
    array = list(map(int, input().split()))
    b.append(array)


group1 = []
group2 = []
group3 = []

for i in range(n):
    for j in range(n):
        if (i + j) % 3 == 0:
            group1.append(b[i][j])
        elif (i + j) % 3 == 1:
            group2.append(b[i][j])
        else:
            group3.append(b[i][j])

g1 = Counter(group1)
g2 = Counter(group2)
g3 = Counter(group3)

sum1 = []
sum2 = []
sum3 = []


def cal(l, output):
    for i in range(c):
        s = 0
        for j in l.items():
            s += a[j[0]-1][i-1] * j[1]
        output.append(s)


li = [g1, g2, g3]
li2 = [sum1, sum2, sum3]

for i in range(3):
    cal(li[i], li2[i])

cc = list(range(c))

min_sum = 10**10
for i, j, k in permutations(cc, 3):
    sum = sum1[i] + sum2[j] + sum3[k]
    if sum < min_sum:
        min_sum = sum

print(min_sum)

Verdicts: Wrong Answer

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
import itertools
import sys
input = sys.stdin.readline()


def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]

    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]

    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)

    print(res)

main()

import itertools
import sys
input = sys.input.readline()


def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]

    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]

    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)

    print(res)

main()


from itertools import product, permutations

N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]
grid = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]

cost = [[0] * C for _ in range(3)]

for x, y in product(range(N), range(N)):
    cost[(x + y) % 3][grid[x][y]] += 1

minn = float("inf")
for colors in permutations(range(C), 3):
    t = 0
    for s, color_n in enumerate(colors):
        for color_p, n in enumerate(cost[s]):
            t += n * D[color_p][color_n]
    minn = min(minn, t)
print(minn)


Verdicts: Runtime Error

Runtime Error

Accepted
Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
from itertools import permutations

n, c = map(int, input().split())
change_cost = [list(map(int, input().split())) for i in range(c)]

iwakan = {}
for i in range(3):
    iwakan[i] = [0]*c

for i in range(n):
    for j, color in enumerate(list(map(int, input().split()))):
        print(i,j, color)
        for k in range(c):
            iwakan[(i+j)%3][k] += change_cost[color-1][k]

ans = float("inf")
for p in permutations(range(c), 3):
    cost = 0
    for i, color in enumerate(p):
        cost += iwakan[i][color]
    ans = min(ans, cost)
print(ans)

import numpy as np
from itertools import permutations

n, c = map(int, input().split())
change_cost = [np.array(list(map(int, input().split()))) for i in range(c)]

iwakan = {}
for i in range(3):
    iwakan[i] = np.zeros(c)

for i in range(n):
    for j, color in enumerate(list(map(int, input().split()))):
        iwakan[(i+j)%3] += change_cost[color-1]

ans = float("inf")
for p in permutations(range(c), 3):
    cost = 0
    for i, color in enumerate(p):
        cost += iwakan[i][color]
    ans = min(ans, cost)
print(int(ans))

Verdicts: Wrong Answer

Accepted