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 = list(map(int,input().split()))
D = [ list(map(int,input().split()))  for i in range(C)]
c = [ list(map(int,input().split()))  for i in range(N)]

sep = {0:[0 for i in range(C)],1:[0 for i in range(C)],2:[0 for i in range(C)]}
for i,cx in enumerate(c):
    for j,cy in enumerate(cx):
        if len(c) == 2 and (i + j) % 3 == 2:
            sep[(i + j) % 3 - 1][cy -1] += 1
        else:
            sep[(i + j) % 3][cy - 1] += 1
minimums = [[],[],[]]
for key,values in sep.tems():
    for i in range(C):
        m0 = 0
        am0 = 0
        m1 = 0
        am1 = 1
        deposit = 0
        for j,value in enumerate(values):
            deposit += D[j][i] * valuea
        sep[key].append(deposit)
min0 = min(sep[0])
min1 = min(sep[1])
min2 = min(sep[2])

print(min1 + min2 + min0)





import numpy as np
N,C = map(int,input().split())
a = np.zeros((3,C))
D = np.array([ list(map(int,input().split()))) for i in range(C)])
c = np.array([ list(map(int,input().split()))) for i in range(N)])


for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

sum_arr = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                sum_arr.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))


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

sep = {0:[0 for i in range(C)],1:[0 for i in range(C)],2:[0 for i in range(C)]}
for i,cx in enumerate(c):
    for j,cy in enumerate(cx):
        sep[(i + j) % 3][cy] + = 1
minimums = [[],[],[]]
for key,values in sep.tems():
    for i in range(C):
        m0 = 0
        am0 = 0
        m1 = 0
        am1 = 1
        deposit = 0
        for j,value in enumerate(values):
            deposit += D[j][i] * valuea
        sep[key].append(deposit)
min0 = min(sep[0])

min1 = min(sep[1])
min2 = min(sep[2])
min3 = min(sep[2])

print(min1 + min2 + min3)





import numpy as np
N,C = map(int,input().split())
a = np.zeros((3,C))
D = np.array([ list(map(int,input().split())) for i in range(C)])
c = np.array([ list(map(int,input().split())) for i in range(N)])


for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

sum_arr = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                sum_arr.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(sum_arr)))


Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted