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):
def main():
#    N, C = 2, 3
#    D = [[0, 1, 1],
#         [1, 0, 1],
#         [1, 4, 0]]
#    c_init = [[1, 2],
#              [3, 3]]
    
#    N, C = 4, 3
#    D = [[0, 12, 71],
#         [81, 0, 53],
#         [14, 92, 0]]
#    c_init = [[1, 1, 2, 1],
#              [2, 1, 1, 2],
#              [2, 2, 1, 3],
#              [1, 1, 2, 2]]
    
    N, C = map(int, input().split())
    D = []
    for i in range(C):
        D.append(list(map(int, input().split())))
        
    c_init = []
    for i in range(N):
        c_init.append(list(map(int,input().split())))
    
    group0 = [0]*C
    group1 = [0]*C
    group2 = [0]*C
    
    
    
    for i in range(N):
        for j in range(N):
            if (i+j+2)%3==0:
                group0[c_init[i][j] - 1] += 1
            elif (i+j+2)%3 == 1:
                group1[c_init[i][j] - 1] += 1
            else:
                group2[c_init[i][j] - 1] += 1
                
    print(group0)
    print(group1)
    print(group2)
    
    
    iwakan =[]
    for color0 in range(C):
        for color1 in range(C):
            if color1 == color0:
                continue
            
            for color2 in range(C):
                if color2==color0 or color2==color1:
                    continue
                
                iwakan_temp = 0
                for colorN in range(C):
                    iwakan_temp += group0[colorN]*D[colorN][color0]
                    iwakan_temp += group1[colorN]*D[colorN][color1]
                    iwakan_temp += group2[colorN]*D[colorN][color2]
                
                iwakan.append(iwakan_temp)
                
    print(min(iwakan))
    
if __name__ == "__main__":
    main()

import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n,c = li()
d = [list(li()) for _ in range(c)]
initial = [list(li_()) for _ in range(n)] 

mod_col = [[0]*c for _ in range(3)]

for i,initi in enumerate(initial):
    for j, initij in enumerate(initi):
        mod_col[(i+j) % 3][initij] += 1
        
ans = float('inf')
colors = [-1]*3
for c0 in range(c):
    for c1 in range(c):
        for c2 in range(c):
            if c0 == c1 or c1 == c2 or c2 == c0:
                continue
            
            colors = [c0, c1, c2]
            
            tmp = 0
            for modnum in range(3):
                tmp += sum([d[color][colors[modnum]] * mod_col[modnum][color] for color in range(c)])
                
            ans = min(ans, tmp)


print(ans)

Verdicts: Wrong Answer

Accepted