Problem:
 Score : 400 points    Problem Statement 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.      Constraints  1 \leq N \leq 500  3 \leq C \leq 30  1 \leq D_{i,j} \leq 1000 (i \neq j),D_{i,j}=0 (i=j)  1 \leq c_{i,j} \leq C  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  C  D_{1,1}  ...  D_{1,C}  :  D_{C,1}  ...  D_{C,C}  c_{1,1}  ...  c_{1,N}  :  c_{N,1}  ...  c_{N,N}       Output If the minimum possible sum of the wrongness of all the squares is x , print x .        Sample Input 1 2 3
0 1 1
1 0 1
1 4 0
1 2
3 3      Sample Output 1 3   Repaint (1,1) to Color 2 . The wrongness of (1,1) becomes D_{1,2}=1 .  Repaint (1,2) to Color 3 . The wrongness of (1,2) becomes D_{2,3}=1 .  Repaint (2,2) to Color 1 . The wrongness of (2,2) becomes D_{3,1}=1 .   In this case, the sum of the wrongness of all the squares is 3 .  Note that D_{i,j} \neq D_{j,i} is possible.       Sample Input 2 4 3
0 12 71
81 0 53
14 92 0
1 1 2 1
2 1 1 2
2 2 1 3
1 1 2 2      Sample Output 2 428

Solution (Python):
def min3(x, y, z):
    inf = 10 ** 9
    m1 = min(x)
    m2 = min(y)
    m3 = min(z)
    i1 = x.index(m1)
    i2 = y.index(m2)
    i3 = z.index(m3)
    if abs((i1 - i2) * (i2 - i3) * (i3 - i1)):
        return m1 + m2 + m3
    re1 = re2 = re3 = inf
    if i1 == i2 or i1 == i3:
        x[i1] = inf
        re1 = min3(x, y, z)
        x[i1] = m1
    if i1 == i2 or i2 == i3:
        y[i2] = inf
        re2 = min3(x, y, z)
        y[i2] = m2
    if i3 == i2 or i1 == i3:
        z[i3] = inf
        re3 = min3(x, y, z)
        z[i3] = m3
    return min(re1, re2, re3)

def dot(A, B):
    C = []
    for x in zip(*B):
        s = 0
        for y, z in zip(A, x):
            s += y * z
        C.append(s)
    return C

gs, cn = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(cn)]
c = [list(map(int, input().split())) for _ in range(gs)]
cc = [[0] * cn for _ in range(3)]
for i in range(gs):
    ci = c[i]
    for j in range(gs):
        cc[(i + j) % 3][ci[j] - 1] += 1
ds = []
for i in range(3):
    ds.append(dot(cc[i], d))
print(min3(ds[0], ds[1], ds[2]))

Solution (Python):
N, C = map(int, input().split())
D = [[]]
for _ in range(C):
    di = [0] + [int(s) for s in input().split()]
    D.append(di)
c = []
for _ in range(N):
    ci = [int(s) for s in input().split()]
    c.append(ci)

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

ans = float("inf")
for c0 in range(1, C + 1):
    for c1 in range(1, C + 1):
        for c2 in range(1, C + 1):
            if c0 == c1 or c1 == c2 or c2 == c0:
                continue
            d = 0
            d += sum(cnt[0][v] * D[v][c0] for v in range(1, C + 1))
            d += sum(cnt[1][v] * D[v][c1] for v in range(1, C + 1))
            d += sum(cnt[2][v] * D[v][c2] for v in range(1, C + 1))
            ans = min(ans, d)
print(ans)

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

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

res = 10 ** 9
p = [0] * 3
for p[0] in range(C):
    for p[1] in range(C):
        if p[0] == p[1]:
            continue
        for p[2] in range(C):
            if p[2] == p[0] or p[1] == p[2]:
                continue
            tmp = 0
            for i in range(3):
                tmp += cost[i][p[i]]
            res = min(res, tmp)

print(res)

Solution (Python):
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)]

from itertools import permutations

colors = tuple(i for i in range(C))

table = [[0 for i in range(C)] for j in range(3)]
for color in colors:
    for col in range(N):
        for row in range(N):
            table[(col+row)%3][color]+=D[c[col][row]-1][color]
            
ans = 10**9    
for i, j, k in permutations(colors, 3):
    ans = min(ans, table[0][i]+table[1][j]+table[2][k])

print(ans)

Solution (Python):
N, C = map(int, input().split())

weight = []
color = []
colorCount = [[0 for _ in range(C)] for _ in range(3)]

for _ in range(C) :
    line = list(map(int, input().split()))
    weight.append(line)

for i in range(N) :
    line = list(map(int, input().split()))
    color.append(line)
    for j, c in enumerate(line) :
        colorCount[(i + j + 2) % 3][c - 1] += 1

ans = float('inf')

for firstColor in range(C) :
    for secondColor in range(C) :
        for thirdColor in range(C) :
            if firstColor == secondColor or secondColor == thirdColor or thirdColor == firstColor :
                continue
            cost = 0
            for i, newColor in enumerate([firstColor, secondColor, thirdColor]) :
                for oldColor in range(C) :
                    cost += colorCount[i][oldColor] * weight[oldColor][newColor]
            ans = min(ans, cost)

print(ans)

Solution (Python):
def solve(p, query):
    
    group_a = [0]*(color+1)
    group_b = [0]*(color+1)
    group_c = [0]*(color+1)

    for c_a, c_b, c_c in zip(mod_1, mod_2, mod_0):
        for i in range(1, color+1):
            group_a[i] += d[c_a][i]
            group_b[i] += d[c_b][i]
            group_c[i] += d[c_c][i]

    top_3_group_a = sorted(
        list(zip(list(range(color+1)), group_a[1:])), key=lambda x: x[1])[:3]
    top_3_group_b = sorted(
        list(zip(list(range(color+1)), group_b[1:])), key=lambda x: x[1])[:3]
    top_3_group_c = sorted(
        list(zip(list(range(color+1)), group_c[1:])), key=lambda x: x[1])[:3]
    ans = float('inf')
    for i in top_3_group_a:
        for j in top_3_group_b:
            if i[0] != j[0]:
                for k in top_3_group_c:
                    if i[0] != k[0] and j[0] != k[0]:
                        ans = min(ans, i[1]+j[1]+k[1])
    return ans


if __name__ == '__main__':
    n, color = map(int, input().split())
    c, d = [], []
    d.append([0]*(color+1))

    for _ in range(color):
        d.append([0]+list(map(int, input().split())))

    _c = []
    for _ in range(n):
        _c.append(list(map(int, input().split())))

    mod_1, mod_2, mod_0 = [], [], []

    for i in range(n):
        for j in range(n):
            if (i+j) % 3 == 1:
                mod_1.append(_c[i][j])
            elif (i+j) % 3 == 2:
                mod_2.append(_c[i][j])
            else:
                mod_0.append(_c[i][j])

    if n % 3 == 1:
        mod_1.append(0)
        mod_2.append(0)
    if n % 3 == 2:
        mod_0.append(0)
        mod_2.append(0)

    print(solve(c, d))

Solution (Python):
from collections import defaultdict
N, C = map(int, input().split())
D = [list(map(int, input().split())) for i in range(C)]
cnt = [defaultdict(int) for i in range(3)]
for i in range(N):
    s = list(map(int, input().split()))
    for j in range(N):
        t = (i+j+2) % 3
        cnt[t][s[j]-1] += 1
ans = 10**9
for x in range(C):
    for y in range(C):
        for z in range(C):
            if x == y or y == z or z == x: continue
            tmp = 0
            for i, c in enumerate([x, y, z]):
                for key in cnt[i]:
                    tmp += D[key][c] * cnt[i][key]
            ans = min(ans, tmp)
print(ans)

Solution (Python):
N, C = map(int, input().split())
D = []
for i in range(0, C):
	D.append(list(map(int, input().split())))
colors = [[], [], []]
for i in range(0, N):
	A = list(map(lambda v: int(v)-1, input().split()))
	for j in range(0, N):
		colors[(i+j)%3].append(A[j])
sums = [[], [], []]
for i in range(0, 3):
	for j in range(0, C):
		sum = 0
		for k in range(0, len(colors[i])):
			sum += D[colors[i][k]][j]
		sums[i].append(sum)
min = 5000000000000000
for i in range(0, C):
	for j in range(0, C):
		if i != j:
			for k in range(0, C):
				if i != k and j != k:
					if sums[0][i] + sums[1][j] + sums[2][k] < min:
						min = sums[0][i] + sums[1][j] + sums[2][k]
print(min)

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

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

"""
3色で塗る
"""
cost_c1 = []
cost_c2 = []
cost_c3 = []
for Y in range(C):
    
    cost = 0
    for X in C1:
        cost += Dij[X-1][Y]
        
    cost_c1.append(cost)
    
    cost = 0
    for X in C2:
        cost += Dij[X-1][Y]
        
    cost_c2.append(cost)
    
    cost = 0
    for X in C3:
        cost += Dij[X-1][Y]
        
    cost_c3.append(cost)



minA = 0

ans = float("inf")
for i,c1 in enumerate(cost_c1):
    for j,c2 in enumerate(cost_c2):
        for k,c3 in enumerate(cost_c3):
            if i!=j and j!=k and k!=i:
                ans = min(ans,c1+c2+c3) 
print(ans)

Solution (Python):
from collections import defaultdict

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

gg = [defaultdict(int) for _ in range(3)]
for i in range(n):
    g = list(map(int, input().split()))
    for j in range(n):
        gg[(i+j)%3][g[j]-1] += 1

ans = max(max(d)) * n**2
for c0 in range(c):
    for c1 in range(c):
        if c1 == c0:
            continue
        for c2 in range(c):
            if c0 == c2 or c1 == c2:
                continue

            tmp_ans = sum([d[i][c0] * gg[0][i] for i in gg[0]]) \
                    + sum([d[i][c1] * gg[1][i]  for i in gg[1]]) \
                    + sum([d[i][c2] * gg[2][i]  for i in gg[2]])
            
            ans = min(ans, tmp_ans)

print(ans)

Solution (Python):
import sys
import itertools
MOD = 3
INF = 1000000000


def input():
    return sys.stdin.readline().rstrip()


def main():
    n, c = [int(e) for e in input().split()]
    dMat = [[int(e) for e in input().split()] for _ in range(c)]
    cMat = [[int(e) for e in input().split()] for _ in range(n)]

    cost = [[0 for _ in range(c)] for _ in range(MOD)]
    for hi in range(n):
        for wi in range(n):
            cMod = ((hi + 1) + (wi + 1)) % MOD
            for ci in range(c):
                cost[cMod][ci] += dMat[cMat[hi][wi] - 1][ci]

    ans = INF
    for perm in itertools.permutations(range(c), MOD):
        costSum = 0
        for di in range(MOD):
            costSum += cost[di][perm[di]]
        ans = min(ans, costSum)
    print(ans)


main()

Solution (Python):
import sys
import itertools
import numpy as np
MOD = 3
INF = 1000000000


def input():
    return sys.stdin.readline().rstrip()


def main():
    n, c = [int(e) for e in input().split()]
    dMat = [[int(e) for e in input().split()] for _ in range(c)]
    cMat = [[int(e) for e in input().split()] for _ in range(n)]

    cost = [[0 for _ in range(c)] for _ in range(MOD)]
    for hi in range(n):
        for wi in range(n):
            cMod = ((hi + 1) + (wi + 1)) % MOD
            for ci in range(c):
                cost[cMod][ci] += dMat[cMat[hi][wi] - 1][ci]

    ans = INF
    for perm in itertools.permutations(range(c), MOD):
        costSum = 0
        for di in range(MOD):
            costSum += cost[di][perm[di]]
        ans = min(ans, costSum)
    print(ans)


main()

Solution (Python):
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)]

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

iwakan_all = []
for i in range(3):
    _init_colors = init_colors[i]
    _iwakan = []

    for target in range(C):

        iwakan = 0
        for j in range(len(_init_colors)):
            iwakan += d[_init_colors[j] - 1][target - 1]

        _iwakan.append(iwakan)

    iwakan_all.append(_iwakan)


res = float('inf')
for i in range(C):
    tmp1 = iwakan_all[0][i]
    for j in range(C):
        if j != i:
            tmp2 = iwakan_all[1][j]
            for k in range(C):
                if k != i and k != j:
                    tmp3 = iwakan_all[2][k]
                    res = min(res, tmp1 + tmp2 + tmp3)

print(res)

Solution (Python):
N, C = 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)]
col = [[0]*3 for i in range(C)]
for i in range(N):
    for j in range(N):
        p = c[i][j]-1
        for k in range(C):
            col[k][(i+j)%3] += D[p][k]
ans = 10**18
for i in range(C):
    for j in range(C):
        if i == j:
            continue
        for k in range(C):
            if i == k or j == k:
                continue
            ans = min(ans, col[i][0] + col[j][1] + col[k][2])
print(ans)

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

dic0 = {}
dic1 = {}
dic2 = {}

for i in range(N) :
    for j in range(N) :
        key = C[i][j]-1
        if (i+j) % 3 == 0 :
            if key not in dic0 :
                dic0[key] = 1
            else :
                dic0[key] += 1
        elif (i+j) % 3 == 1 :
            if key not in dic1 :
                dic1[key] = 1
            else :
                dic1[key] += 1
        else :
            if key not in dic2 :
                dic2[key] = 1
            else :
                dic2[key] += 1

ans = float("inf")

for i in range(M) :
    for j in range(M) :
        if i == j :
            continue
        for k in range(M) :
            if (i == k) or (j == k) :
                continue
            
            calc = 0
            for a,b in dic0.items() :
                calc += D[a][i]*b
            for a,b in dic1.items() :
                calc += D[a][j]*b
            for a,b in dic2.items() :
                calc += D[a][k]*b
            ans = min(ans,calc)
print(ans)

Solution (Python):
def calc_total_D(stats, D, color):
    total = 0
    for i in range(3):
        y = color[i]
        for x in range(len(stats[i])):
            total += stats[i][x] * D[x][y]
    return total


def main():
    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)]

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

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

    retval = float("inf")
    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
                retval = min(retval, calc_total_D(stats, D, [c1, c2, c3]))
    print(retval)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
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)]

c0=Counter([c[i][j] for i in range(N) for j in range(N) if (i+j)%3==0])
c1=Counter([c[i][j] for i in range(N) for j in range(N) if (i+j)%3==1])
c2=Counter([c[i][j] for i in range(N) for j in range(N) if (i+j)%3==2])

d0,d1,d2=dict(),dict(),dict()
for i in range(C):
	d0[i]=sum([c0[j+1]*D[j][i] for j in range(C)])
	d1[i]=sum([c1[j+1]*D[j][i] for j in range(C)])
	d2[i]=sum([c2[j+1]*D[j][i] for j in range(C)])

min_=10**9
for i in range(C):
	for j in range(C):
		for k in range(C):
			if i!=j and j!=k and k!=i:
				if min_>d0[i]+d1[j]+d2[k]:
					min_=d0[i]+d1[j]+d2[k]
print(min_)

Solution (Python):
n,c=map(int,input().split())
d=[list(map(int,input().split())) for _ in range(c)]
board=[list(map(int,input().split())) for _ in range(n)]
cnt=[[0]*c for _ in range(3)]
for i in range(n):
  for j in range(n):
    cnt[(i+j)%3][board[i][j]-1]+=1
ans=10**10
for i in range(c):
  for j in range(c):
    if i==j:
      continue
    for k in range(c):
      if i==k or j==k:
        continue
      tmp=0
      for l in range(c):
        tmp+=d[l][i]*cnt[0][l]
        tmp+=d[l][j]*cnt[1][l]
        tmp+=d[l][k]*cnt[2][l]
      ans=min(ans,tmp)
print(ans)

Solution (Python):
N,C=map(int,input().split())
D=[list(map(int,input().split())) for i in range(C)]
MAP=[list(map(int,input().split())) for i in range(N)]
from collections import defaultdict
cnt=[defaultdict(int) for i in range(3)]
for i in range(N):
    for j in range(N):
        cnt[(i+j)%3][MAP[i][j]-1]+=1
ans=float('inf')
cost=[[0]*C for i in range(3)]
for i in range(3):
    for y in range(C):
        for x in range(C):
            cost[i][y]+=D[x][y]*cnt[i][x]
for x in range(C):
    for y in range(C):
        if x==y:
            continue
        for z in range(C):
            if z==x or z==y:
                continue
            ans=min(ans,cost[0][x]+cost[1][y]+cost[2][z])
print(ans)

Solution (Python):
import itertools
n,c = map(int,input().split())
D = []
for i in range(c):
    L = list(map(int,input().split()))
    D.append(L)
G = []
for i in range(n):
    P = list(map(int,input().split()))
    G.append(P)
mod3_0 = [0]*c
mod3_1 = [0]*c
mod3_2 = [0]*c
for i in range(n):
    for j in range(n):
        if (i+j+2)%3 == 0:
            mod3_0[G[i][j]-1] += 1
        elif (i+j+2)%3 == 1:
            mod3_1[G[i][j]-1] += 1
        else:
            mod3_2[G[i][j]-1] += 1
t = []
for i in range(c):
    t.append(i)
T = list(itertools.permutations(t,3))
ans = float('inf')
for i in range(len(T)):
    cur = 0
    for j in range(c):
        cur +=  mod3_0[j]*(D[j][T[i][0]])
    for k in range(c):
        cur +=  mod3_1[k]*(D[k][T[i][1]])
    for l in range(c):
        cur +=  mod3_2[l]*(D[l][T[i][2]])
    if ans >= cur:
        ans = cur
print(ans)

Solution (Python):
def inverse(a):
    return pow(a, mod - 2, mod)


def usearch(x, a):
    lft = 0
    rgt = len(a) + 1
    while rgt - lft > 1:
        mid = (rgt + lft) // 2
        if a[mid] <= x:
            lft = mid
        else:
            rgt = mid
    return lft


def main():
    n,C=map(int, input().split())
    d = [0] * C
    for i in range(C):
        d[i] = [int(j) for j in input().split()]
    c = [0] * n
    for i in range(n):
        c[i] = [int(j)-1 for j in input().split()]
    cost = [[0] * C for _ in range(3)]
    for x in range(n):
        for y in range(n):
            for target in range(C):
                cost[(x+y)%3][target] += d[c[x][y]][target]
    ans = 1e9
    for i in range(C):
        for j in range(C):
            for k in range(C):
                if len({i, j, k}) < 3:
                    continue
                ans = min([ans, cost[0][i] + cost[1][j] + cost[2][k]])
    print(ans)

main()

Solution (Python):
N,C = 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)]
m0 = []
m1 = []
m2 = []
for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            m0.append(c[i][j])
        elif  (i+j+2)%3 == 1:
            m1.append(c[i][j])
        else:
            m2.append(c[i][j])
cost = [[0]*C for i in range(3)]
for m in m0:
    for color in range(C):
        cost[0][color] += D[m-1][color] 
for m in m1:
    for color in range(C):
        cost[1][color] += D[m-1][color] 
for m in m2:
    for color in range(C):
        cost[2][color] += D[m-1][color] 

res = 10**10
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
            res = min(cost[0][i]+cost[1][j]+cost[2][k],res)
print(res)

Solution (Python):
from itertools import permutations as perm
N,C = map(int, input().split())
colors = []
for i in range(C):
    colors.append(list(map(int, input().split())))

grids = {0:{}, 1:{}, 2:{}}
for i in range(1,N+1):
    A =list(map(int, input().split()))
    for j in range(N):
        if A[j] not in grids[(i+j+1)%3]:
            grids[(i+j+1)%3][A[j]] = 1
        else:
            grids[(i+j+1)%3][A[j]] += 1
mn = 10**9
for color in perm([i for i in range(1,C+1)], 3):
    tmp = 0
    for i in range(3):
        for k,v in grids[i].items():
            tmp += colors[k-1][color[i]-1] * v
    mn = min(mn, tmp)
print(mn)

Solution (Python):
import sys

from collections import deque

from collections import defaultdict

import heapq

import bisect





s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

"""
マスは1つの色を持つ -> c[i][j]
マスは3グループのいずれかに属する -> (i+j)%3
グループは1つの色 gc を持つ、ただし他のグループと同じ色を持てない gc1 != gc2 != gc3
マス違和感 = D[マスの色][グループの色]
グループ違和感 = グループ内のマスの、マス違和感の合計 = DG[グループ色]
全体違和感 = 3グループの、グループ違和感の合計
答え = 全体違和感
"""
def main():
    N, C = i2nn()
    D = ii2nnn(C)
    c = ii2nnn(N)
    iwakanG = [[0] * C for _ in range(3)]  
    for i in range(N):
        for j in range(N):
            groupID = (i + j) % 3  
            cM = c[i][j] - 1  
            for cG in range(C):
                iwakanM = D[cM][cG]   
                iwakanG[groupID][cG] += iwakanM  
    iwakan = 1e+9
    for gc1 in range(C):
        for gc2 in range(C):
            for gc3 in range(C):
                if gc1 == gc2 or gc2 == gc3 or gc3 == gc1:
                    continue
                n = iwakanG[0][gc1] + iwakanG[1][gc2] + iwakanG[2][gc3]
                if iwakan > n:
                    iwakan = n  
    print(iwakan)

main()

Solution (Python):
import numpy as np
N,C = map(int,input().split())
D = np.array([list(map(int,input().split())) for _ in range(C)])
c = np.array([list(map(int,input().split())) for _ in range(N)])

strange = np.array([[0]*C for _ in range(3)])
for i in range(N):
    for j in range(N):
        strange[(i+j)%3] += D[c[i][j]-1]

ans = 1000*N**2
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            if c1 != c2 and c2 != c3 and c3 != c1:
                ans = min(ans,strange[0][c1]+strange[1][c2]+strange[2][c3])
print(ans)

Solution (Python):
from collections import defaultdict
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)]

strange = [defaultdict(lambda:0) for _ in range(3)]
for i in range(N):
    for j in range(N):
        for color in range(C):
            strange[(i+j)%3][color] += D[c[i][j]-1][color]

ans = 1000*N**2
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            if c1 != c2 and c2 != c3 and c3 != c1:
                ans = min(ans,strange[0][c1]+strange[1][c2]+strange[2][c3])
print(ans)

Solution (Python):

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

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

points = []
for i in range(3):
    row = []
    for color in range(1, c+1):
        x = 0
        for j, m in enumerate(grid[i]):
            x += m * D[j][color-1]
        row.append([x, color])
    points.append(row)

for i in range(3):
    points[i].sort()

ans = 1000*n*n
for i in range(27):
    x = points[0][i//9]
    y = points[1][i//3%3]
    z = points[2][i%3]
    if x[1] == y[1] or y[1] == z[1] or z[1] == x[1]:
        continue
    ans = min(ans, x[0]+y[0]+z[0])
print(ans)

Solution (Python):
N, C = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(C)]
c_map = [list(map(int, input().split())) for _ in range(N)]
count = [[0]*C for _ in range(3)]
for i in range(N):
    for j in range(N):
        c = c_map[i][j]
        amari = (i+j)%3
        count[amari][c-1]+=1
ans = float('inf')
for i in range(C):
    for j in range(C):
        for k in range(C):
            tmp = 0
            if i == k or j == k or i==j:
                continue
            else:
                for l in range(C):
                    tmp+=count[0][l]*D[l][i]
                    tmp+=count[1][l]*D[l][j]
                    tmp+=count[2][l]*D[l][k]
            ans = min(ans, tmp)
print(ans)

Solution (Python):
N, C = 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)]
cnt = [[0] * C for i in range(3)]
for i in range(N):
    for j in range(N):
        cnt[(i + j + 2) % 3][c[i][j] - 1] += 1
ans = float('inf')
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
            tmp = 0
            for i in range(3):
                for j in range(C):
                    tmp += d[j][[c1, c2, c3][i]] * cnt[i][j]
            ans = min(ans, tmp)
print(ans)

Solution (Python):
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)

Solution (Python):
n,c = map(int,input().split())
d = [list(map(int,input().split())) for _ in range(c)]

a = [[0]*c for _ in range(3)]
b = [[[0,i] for i in range(c)] for _ in range(3)]

for i in range(n):
  l = list(map(int,input().split()))
  for j in range(n):
    a[(i+j)%3][l[j]-1] += 1
    
for i in range(3):
  for k in range(c):
    b[i][k][0] = sum([a[i][t]*d[t][k] for t in range(c)])


p = [[],[],[]]
for i in range(3):
  p[i] = sorted(b[i])[:3]
m = []
for t in p[0]:
  for s in p[1]:
    for u in p[2]:
      if (t[1] != s[1]) and (t[1] != u[1]) and (s[1] != u[1]):
        m.append(t[0]+s[0]+u[0])
print(min(m))

Solution (Python):
import sys
input = sys.stdin.readline
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)]

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

for i_y in range(N):
    for i_x in range(N):
        colorcnt[(i_x + i_y) % 3][c[i_y][i_x] - 1] += 1


            
pentab = [[0] * C for _ in range(3)]
for grp in range(3):
    for c_dst in range(C):
        penalty = 0
        for c_src in range(C):
            penalty += colorcnt[grp][c_src] * D[c_src][c_dst]
        pentab[grp][c_dst] = penalty



import itertools

ans = 10 ** 18
for c_ans in itertools.permutations(range(C), 3):
    ans_tmp = sum([pentab[i][c_ans[i]] for i in range(3)])
    if ans > ans_tmp:
        ans = ans_tmp

print(ans)

Solution (Python):
N, C = map(int, input().split())
D = [0] * C
for i in range(C):
    D[i] = list(map(int, input().split()))
grid = [0] * N
for i in range(N):
    grid[i] = list(map(int, input().split()))

cost0 = [0] * C
cost1 = [0] * C
cost2 = [0] * C
for color in range(C):
    for h in range(N):
        for w in range(N):
            mod = (h + w) % 3
            if mod == 0:
                cost0[color] += D[grid[h][w] - 1][color]
            elif mod == 1:
                cost1[color] += D[grid[h][w] - 1][color]
            else:
                cost2[color] += D[grid[h][w] - 1][color]

ans = float('inf')
for i in range(C):
    j_list = list(range(C))
    j_list.remove(i)
    for j in j_list:
        k_list = list(range(C))
        k_list.remove(i)
        k_list.remove(j)
        for k in k_list:
            ans = min(ans, cost0[i] + cost1[j] + cost2[k])
print(ans)

Solution (Python):
from collections import defaultdict

n, c = map(int, input().split())
D = [list(map(int, input().split())) for _ in range(c)]
p = [defaultdict(int), defaultdict(int), defaultdict(int)]
for i in range(n):
    ci = list(map(int, input().split()))
    for j, cj in enumerate(ci):
        k = (i + j + 2) % 3
        p[k][cj] += 1
ans = []
for i in range(1, c + 1):
    x = 0
    for ci, ti in p[0].items():
        x += D[ci - 1][i - 1] * ti

    for j in range(1, c + 1):
        if j == i:
            continue
        y = 0
        for cj, tj in p[1].items():
            y += D[cj - 1][j - 1] * tj

        for k in range(1, c + 1):
            if k in [i, j]:
                continue
            z = 0
            for ck, tk in p[2].items():
                z += D[ck - 1][k - 1] * tk
            ans.append(x + y + z)

print(min(ans))

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

di = [{}, {}, {}]

for i in range(N) :
    for j in range(N) :
        num = (i+j) % 3
        before = L[i][j] - 1
        
        if before not in di[num] :
            di[num][before] = 1
        else :
            di[num][before] += 1

B = []

for d in di :
    A = []
    for after in range(C) :
        calc = 0
        for before in d :
            calc += D[before][after] * d[before]
        A.append([calc,after])

    A = sorted(A,key=lambda x:x[0],reverse=False)[:3]
    B.append(A)
    
ans = float("inf")

for i in range(3) :
    for j in range(3) :
        for k in range(3) :
            if (B[0][i][1] - B[1][j][1]) * (B[1][j][1] - B[2][k][1]) * (B[2][k][1] - B[0][i][1]) == 0 :
                continue
            
            ans = min(ans, B[0][i][0] + B[1][j][0] + B[2][k][0])

print(ans)