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



Sample input:
2 3
0 1 1
1 0 1
1 4 0
1 2
3 3


Sample output:
3

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,c = inpm()
    D = inpll(c)
    a = inpll(n)
    dic1 = defaultdict(int)
    dic2 = defaultdict(int)
    dic3 = defaultdict(int)
    for i in range(n):
        for j in range(n):
            if (i+j)%3 == 0:
                dic1[a[i][j]] += 1
            elif (i+j)%3 == 1:
                dic2[a[i][j]] += 1
            elif (i+j)%3 == 2:
                dic3[a[i][j]] += 1
    ANS = 10**10
    for e in permutations(range(min(c,3))):
        if c == 2:
            [x, y] = e
            ans = 0
            for color1 in dic1:
                if color1 == x+1:
                    continue
                ans += dic1[color1] * D[color1-1][x]
            for color2 in dic2:
                if color2 == y+1:
                    continue
                ans += dic2[color2] * D[color2-1][y]
            ANS = min(ANS,ans)
        else:
            [x, y, z] = e
            ans = 0
            for color1 in dic1:
                if color1 == x+1:
                    continue
                ans += dic1[color1] * D[color1-1][x]
            for color2 in dic2:
                if color2 == y+1:
                    continue
                ans += dic2[color2] * D[color2-1][y]
            for color3 in dic3:
                if color3 == z+1:
                    continue
                ans += dic3[color3] * D[color3-1][z]
            ANS = min(ANS,ans)
    print(ANS)            

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import itertools
n,c=map(int,input().split())
ld=[list(map(int,input().split())) for i in range(c)]
lc=[list(map(int,input().split())) for i in range(n)]
ct=[[0 for i in range(c)] for i in range(3)]
for i in range(n):
  for j in range(n):
    ct[(i+j+2)%3][lc[i][j]-1]+=1
L=list(itertools.permutations([i for i in range(c)],3))
print(L)
ans=[]
for i in range(len(L)):
  ct2=0
  for j in range(3):
    for k in range(c):
      ct2+=ld[k][L[i][j]]*ct[j][k]
  ans.append(ct2)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n,c=map(int, input().split())
d = [list(map(int, input().split())) for i in range(c)]
a = [list(map(int, input().split())) for i in range(n)]
x=[]
y=[]
z=[]
#３で割ったあまりで分類
for i in range(n):
    for j in range(n):
        if (i+j)%3==0:
            x.append(a[i][j])
        elif (i+j)%3==1:
            y.append(a[i][j])
        else:
            z.append(a[i][j])

#どのいろにするのがコストが安いか上位３種類求める
min1=[[0,0] for i in range(c)]
min2=[[0,0] for i in range(c)]
min3=[[0,0] for i in range(c)]
for i in range(c):
    for k in range(i+1,c):
        for l in range(k+1,c):
            minx=0
            miny=0
            minz=0
            ans=10**11
            for j in range(len(x)):
                minx+=d[x[j]-1][i]
            for j in range(len(y)):
                miny += d[y[j] - 1][k]
            for j in range(len(z)):
                minz += d[z[j] - 1][l]
        ans=min(ans,minx+miny+minz)
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools


def main():
    N, C = map(int, input().split())

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

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

    clazz = [[] for _ in range(3)]

    for i in range(1, N + 1):
        for j in range(1, N + 1):
            clazz[(i + j) % 3].append(CNN[i - 1][j - 1])

    ans = 99999999999999999999
    for colors in itertools.permutations((1, 2, 3)):
        color1, color2, color3 = colors

        ans_temp = 0
        ans_temp += len(list(filter(lambda c: c == 1,
                                    clazz[0]))) * DCC[0][color1 - 1]
        ans_temp += len(list(filter(lambda c: c == 2,
                                    clazz[0]))) * DCC[1][color1 - 1]
        ans_temp += len(list(filter(lambda c: c == 3,
                                    clazz[0]))) * DCC[2][color1 - 1]
        ans_temp += len(list(filter(lambda c: c == 1,
                                    clazz[1]))) * DCC[0][color2 - 1]
        ans_temp += len(list(filter(lambda c: c == 2,
                                    clazz[1]))) * DCC[1][color2 - 1]
        ans_temp += len(list(filter(lambda c: c == 3,
                                    clazz[1]))) * DCC[2][color2 - 1]
        ans_temp += len(list(filter(lambda c: c == 1,
                                    clazz[2]))) * DCC[0][color3 - 1]
        ans_temp += len(list(filter(lambda c: c == 2,
                                    clazz[2]))) * DCC[1][color3 - 1]
        ans_temp += len(list(filter(lambda c: c == 3,
                                    clazz[2]))) * DCC[2][color3 - 1]

        ans = min(ans, ans_temp)

    print(ans)


main()

Submission status: Wrong Answer

Solution code:
from itertools import 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)]

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

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

if N == 1:
    print(0)
    exit()

ans = 10 ** 8
for color in permutations(range(C), 3):
    dis = 0
    for i in range(3):
        for j in range(C):
            d = D[j][color[i]]
            num = cnt[i][j]
            dis += d * num

    ans = min(ans, dis)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import 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)]

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

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


ans = 10 ** 8
for color in permutations(range(C), 3):
    dis = 0
    for i in range(3):
        for j in range(C):
            d = D[j][color[i]]
            num = cnt[i][j]
            dis += d * num

    ans = min(ans, dis)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter

n, c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]
a = [list(map(int, input().split())) for _ in range(n)]
zer, one, two = Counter(), Counter(), Counter()
for i, l in enumerate(a):
	for j, x in enumerate(l):
		x -= 1
		if (i + j) % 3 == 0:
			zer[x] += 1
		elif (i + j) % 3 == 1:
			one[x] += 1
		else:
			two[x] += 1
ans = 1 << 30
for f in range(c):
	cand = sum(d[k][f] * v for k, v in zer.items())
	for s in range(c):
		if f == s:
			continue
		cand += sum(d[k][s] * v for k, v in one.items())
		for t in range(c):
			if f == t or s == t:
				continue
			cand += sum(d[k][t] * v for k, v in two.items())
			if ans > cand:
				ans = cand
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, C = (int(i) for i in 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)]
    base = [[((i+1)+(j+1)) % 3 for j in range(N)] for i in range(N)]

    ans = 1 << 60
    dic = [{0: 1, 1: 2, 2: 3},
           {0: 1, 1: 3, 2: 2},
           {0: 2, 1: 1, 2: 3},
           {0: 2, 1: 3, 2: 1},
           {0: 3, 1: 1, 2: 2},
           {0: 3, 1: 2, 2: 1}]
    for d in dic:
        cur = 0
        for i in range(N):
            for j in range(N):
                color = d[base[i][j]]
                if c[i][j] == color:
                    continue
                else:
                    cur += D[c[i][j]-1][color - 1]
        ans = min(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, C = (int(i) for i in 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)]
    base = [[((i+1)+(j+1)) % 3 for j in range(N)] for i in range(N)]

    ans = 1 << 60
    dic = [{0: 1, 1: 2, 2: 3},
           {0: 3, 1: 1, 2: 2},
           {0: 2, 1: 3, 2: 1}]
    for d in dic:
        cur = 0
        for i in range(N):
            for j in range(N):
                color = d[base[i][j]]
                if color == c[i][j]:
                    continue
                else:
                    cur += D[c[i][j]-1][color - 1]
        ans = min(ans, cur)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,C = map(int,input().split())

d = [0 for i in range(C)]
for i in range(C):
    d_xy =  list(map(int,input().split()))
    d[i] = d_xy

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

li0 = [0 for i in range(C)]
li1 = [0 for i in range(C)]
li2 = [0 for i in range(C)]

for i in range(n):
    for j in range(n):
        a=i+1
        b=j+1
        if (a+b)%3==0:
            li0[c[i][j]-1] += 1
        elif (a+b)%3==1:
            li1[c[i][j]-1] += 1
        elif (a+b)%3==2:
            li2[c[i][j]-1] += 1

ans = (10**3)*500


for c0 in range(C):
    for c1 in range(C):
        for c2 in range(C):
            cst = 0
            if (c0==c1) or (c1==c2) or (c2==c0):
                continue

            for i in range(C):
                cst += d[i][c0]*li0[i]
                cst += d[i][c1]*li1[i]
                cst += d[i][c2]*li2[i]
            ans = min(ans,cst)

print(ans)

Submission status: Wrong Answer

Solution code:
N,C=map(int,input().split())
D=[[int(x) for x in input().split()] for y in range(C)]
c=[[int(x)-1 for x in input().split()] for y in range(N)]

d=[[0 for x in range(C)] for y in range(3)]
for i in range(N):
  for j in range(N):
    d[(i+j)%3][c[i][j]] += 1

a=10**9
for c0 in range(C):
  for c1 in range(c0+1,C):
    for c2 in range(c1+1,C):
      s1,s2,s3=0,0,0
      for c in range(C):
        s1 += d[0][c]*D[c][c0]+d[1][c]*D[c][c1]+d[2][c]*D[c][c2]
        s2 += d[1][c]*D[c][c0]+d[2][c]*D[c][c1]+d[0][c]*D[c][c2]
        s3 += d[2][c]*D[c][c0]+d[0][c]*D[c][c1]+d[1][c]*D[c][c2]
      a=min(a,s1,s2,s3)
print(a)

Submission status: Wrong Answer

Solution code:
n,c = map(int, input().split())
color, grid = [], []
for _ in range(c):
    color.append(list(map(int, input().split())))
grid = {i:{} for i in range(3)}

for i in range(n):
    for j,t in enumerate(map(int, input().split())):
        t -= 1
        if t not in grid[(i+j + 2) % 3]:
            grid[(i+j + 2) % 3][t] = 0
        grid[(i+j + 2) % 3][t] += 1

costs = [[0 for _ in range(c)],[0 for _ in range(c)],[0 for _ in range(c)]]
for cc in range(c):
    for i in range(3):
        for d,cnt in grid[i].items():
            costs[i][cc] += color[d][cc] * cnt

x = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]
y = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]
z = sorted(list(range(c)), key=lambda x: costs[0][x])[:3]
mn = 10**9
for i in x:
    for j in y:
        for k in z:
            if i == j or j == k or k == i:
                continue
            if mn > costs[0][i] + costs[1][j] + costs[2][k]:
                mn = costs[0][i] + costs[1][j] + costs[2][k]
print(mn)

Submission status: Wrong Answer

Solution code:
import itertools
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))

N, C = inpl()
D = [inpl() for _ in range(C)]
c = [inpl() for _ in range(N)]

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

strangeness = [[sum(D[oldc][newc]*nc[k][oldc] for oldc in range(C)) 
                for newc in range(C)] for k in range(3)]
ans = 1000 * N * N
for xyz in itertools.product(range(3),repeat=3):
    x, y, z = xyz
    if x != y and y != z and z != x:
        s = sum(strangeness[k][xyz[k]] for k in range(3))
        if s < ans:
            ans = s
print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
D = [[int(d) for d in input().split()] for _ in range(C)]
clist = [[int(c) for c in input().split()] for _ in range(N)]

L0 = [0]*C
L1 = [0]*C
L2 = [0]*C

for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            L0[clist[i][j]-1] += 1
        elif (i+j+2)%3 == 1:
            L1[clist[i][j]-1] += 1
        else:
            L2[clist[i][j]-1] += 1
            
ans = 10**5
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                c = 0
                for l in range(C):
                    c += L0[l]*D[l][i]
                    c += L1[l]*D[l][j]
                    c += L2[l]*D[l][k]
                ans = min(ans, c)
                
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, C = map(int, input().split())

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

d = [defaultdict(int) for i in range(3)]


for i in range(N):
    c = [int(x) for x in input().split()]
    for j in range(N):
        for k in range(C):
            d[(i+j) % 3][k] += D[c[j]-1][k]
print(d)

ans = 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:
                ans = min(ans, d[0][i]+d[1][j]+d[2][k])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, C = map(int, input().split())
if N == 1:
    print(0)
    exit()
D = [tuple(map(int, input().split())) for _ in range(C)]

d = [defaultdict(int) for i in range(3)]


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

ans = 10**8
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                ans = min(ans, d[0][i]+d[1][j]+d[2][k])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict
N, C = map(int, input().split())
D = [tuple(map(int, input().split())) for _ in range(C)]

d = [defaultdict(int) for i in range(3)]

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

ans = 10**8
for i in range(C):
    for j in range(C):
        for k in range(C):
            if i != j and j != k and k != i:
                ans = min(ans, d[0][i]+d[1][j]+d[2][k])

print(ans)

Submission status: Wrong Answer

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

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

print(color_count)

min_wrongness = 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:
                wrongness = 0
                for l in range(3):
                    for m in range(c):
                        if l == 0:
                            wrongness += D[m][i]*color_count[l][m]
                        elif l == 1:
                            wrongness += D[m][j]*color_count[l][m]
                        else:
                            wrongness += D[m][k]*color_count[l][m]
                min_wrongness = min(min_wrongness,wrongness)
                
print(min_wrongness)

Submission status: Wrong Answer

Solution code:
N,C =[int(x) for x in input().split()]
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]
c1 = []
c2 = []
c3 = []
for i in range(1,N+1):
  for j in range(1,N+1):
    if (i+j)%3==0:
      c1.append(c[i-1][j-1])
    elif (i+j)%3==1:
      c2.append(c[i-1][j-1])
    elif (i+j)%3==2:
      c3.append(c[i-1][j-1])
su = 0
ans = 10000000
import collections as col
c1 = col.Counter(c1)
c2 = col.Counter(c2)
c3 = col.Counter(c3)
#print(c1,c2,c3)
for i in range(C):
  for j in range(i+1,C):
    for k in range(j+1,C):
      for a,b,c in [[i,j,k],[j,i,k],[k,j,i],[i,k,j],[k,i,j],[j,k,i]]:
        su = 0
        for l in range(1,C+1):
          #print(c1[l],c2[l],c3[l])
          su += D[l-1][a]*c1[l]+D[l-1][b]*c2[l]+D[l-1][c]*c3[l]
        ans = min(ans,su)
print(ans)

Submission status: Wrong Answer

Solution code:
N,C =[int(x) for x in input().split()]
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]
c1 = []
c2 = []
c3 = []
for i in range(1,N+1):
  for j in range(1,N+1):
    if (i+j)%3==0:
      c1.append(c[i-1][j-1])
    elif (i+j)%3==1:
      c2.append(c[i-1][j-1])
    elif (i+j)%3==2:
      c3.append(c[i-1][j-1])
su = 0
ans = 1000000
import collections as col
c1 = col.Counter(c1)
c2 = col.Counter(c2)
c3 = col.Counter(c3)
#print(c1,c2,c3)
for i in range(C):
  for j in range(i+1,C):
    for k in range(j+1,C):
      for a,b,c in [[i,j,k],[j,i,k],[k,j,i],[i,k,j],[k,i,j],[j,k,i]]:
        su = 0
        for l in range(1,C+1):
          #print(c1[l],c2[l],c3[l])
          su += D[l-1][a]*c1[l]+D[l-1][b]*c2[l]+D[l-1][c]*c3[l]
        ans = min(ans,su)
print(ans)

Submission status: Wrong Answer

Solution code:
N,C =[int(x) for x in input().split()]
D = [[int(x) for x in input().split()] for _ in range(C)]
c = [[int(x) for x in input().split()] for _ in range(N)]
c1 = []
c2 = []
c3 = []
for i in range(1,N+1):
  for j in range(1,N+1):
    if (i+j)%3==0:
      c1.append(c[i-1][j-1])
    elif (i+j)%3==1:
      c2.append(c[i-1][j-1])
    elif (i+j)%3==2:
      c3.append(c[i-1][j-1])
su = 0
ans = 1000000
for i in range(C):
  for j in range(i+1,C):
    for k in range(j+1,C):
      for a,b,c in [[i,j,k],[j,i,k],[k,j,i],[i,k,j],[k,i,j],[j,k,i]]:
        su = 0
        for l in range(max(len(c1),len(c2),len(c3))):
          su += (0 if l>=len(c1) else D[c1[l]-1][a])+(0 if l>=len(c2) else D[c2[l]-1][b])+(0 if l>=len(c3) else D[c3[l]-1][c])
        ans = min(ans,su)
print(ans)

Submission status: Wrong Answer

Solution code:
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())))
x=[]
y=[]
z=[]
for d in range(C):
  x.append(0)
  y.append(0)
  z.append(0)
for d in range(C):
  for i in range(N):
    for j in range((3-i%3)%3,N,3):
      x[d]+=D[c[i][j]-1][d]
for d in range(C):
  for i in range(N):
    for j in range((1-i%3)%3,N,3):
      y[d]+=D[c[i][j]-1][d]
for d in range(C):
  for i in range(N):
    for j in range((2-i%3)%3,N,3):
      z[d]+=D[c[i][j]-1][d]
num=0
ans=100000000
for i in range(C):
  for j in range(C):
    for k in range(C):
      if i!=j and i!=k and j!=k:
        num=x[i]+y[j]+z[k]
        if num<ans:
          ans=num
print(ans)

Submission status: Wrong Answer

Solution code:
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)
print(c)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

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

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

for i in range(N):
    C_array = map(int, input().split())
    for j, c in enumerate(C_array):
        grid_array[(i+j) % 3][c-1] += 1

print(D_array)

cost_array = [[0] * C for _ in range(3)]
for i in range(3):
    for j in range(C):
        cost = 0
        for k in range(C):
            cost += grid_array[i][k] * D_array[k][j]
        cost_array[i][j] = cost

ans = 10 ** 20

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

Submission status: Wrong Answer

Solution code:
import numpy as np

def min3(A):
    inf = 10 ** 9
    i0 = np.argmin(A[0])
    i1 = np.argmin(A[1])
    i2 = np.argmin(A[2])
    if abs((i1 - i2) * (i2 - i0) * (i0 - i1)):
        return np.min(A[0]) + np.min(A[1]) + np.min(A[2])
    re0 = re1 = re2 = inf
    if i0 == i1 or i0 == i2:
        mn = np.min(A[0])
        A[0][i0] = inf
        re0 = min3(A)
        A[0][i0] = mn
    if i0 == i1 or i1 == i2:
        mn = np.min(A[1])
        A[1][i1] = inf
        re1 = min3(A)
        A[1][i1] = mn
    if i2 == i1 or i0 == i2:
        mn = np.min(A[0])
        A[2][i2] = inf
        re2 = min3(A)
        A[2][i2] = mn
    return min(re0, re1, re2)

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
cc = np.array(cc)
d = np.array(d)
np.dot(cc, d, out=cc)
print(min3(cc))

Submission status: Wrong Answer

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


res = 0
for i in range(N):
    for j in range(N):
        p = (i + j + 2) % 3
        num = 10000
        if p == 0:
            p = 3
        for k in range(p, C+1, 3):
            num = min(num, D[c[i][j]-1][k-1])
        res += num

print(res)

Submission status: Wrong Answer

Solution code:
from itertools import permutations


def solve(p, query):
    # the cost of group's all color change to i
    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:]))[:3], key=lambda x: x[1])
    top_3_group_b = sorted(
        list(zip(list(range(color+1)), group_b[1:]))[:3], key=lambda x: x[1])
    top_3_group_c = sorted(
        list(zip(list(range(color+1)), group_c[1:]))[:3], key=lambda x: x[1])
    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))

Submission status: Wrong Answer

Solution code:
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):
    #1
    cost = 0
    for X in C1:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])
    cost_c1.append(cost)
    #2
    cost = 0
    for X in C2:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])
    cost_c2.append(cost)
    #3
    cost = 0
    for X in C3:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])    
    cost_c3.append(cost)

#1->2->3

def calc(c1,c2,c3):
    tmp1 = [c for c in c1]
    tmp2 = [c for c in c2]
    tmp3 = [c for c in c3]
    min1 = min(tmp1)
    arg1 = tmp1.index(min1)
    tmp2.remove(tmp2[arg1])
    tmp3.remove(tmp3[arg1])
    min2 = min(tmp2)
    arg2 = tmp2.index(min2)
    tmp3.remove(tmp3[arg2])
    min3 = min(tmp3)
    return min1+min2+min3
    
a1 = calc(cost_c1,cost_c2,cost_c3)
a2 = calc(cost_c1,cost_c3,cost_c2)
a3 = calc(cost_c2,cost_c1,cost_c3)
a4 = calc(cost_c2,cost_c3,cost_c1)
a5 = calc(cost_c3,cost_c1,cost_c2)
a6 = calc(cost_c3,cost_c2,cost_c1)

print(min(a1,a2,a3,a4,a5,a6))

Submission status: Wrong Answer

Solution code:
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//3+3):
    x = i * 3
    for j in range(x+1):
        x_,y_ = x-j,j
        if x_ < N and y_ < N:
            # print(x_,y_)
            C1.append(cij[y_][x_])
    x = i * 3 + 1 
    for j in range(x+1):
        x_,y_ = x-j,j
        if x_ < N and y_ < N:
            # print(x_,y_)
            C2.append(cij[y_][x_])
    x = i * 3 + 2
    for j in range(x+1):
        x_,y_ = x-j,j
        if x_ < N and y_ < N:
            # print(x_,y_)
            C3.append(cij[y_][x_])
"""
3色で塗る
"""
cost_c1 = []
cost_c2 = []
cost_c3 = []
for Y in range(C):
    #1
    cost = 0
    for X in C1:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])
    cost_c1.append(cost)
    #2
    cost = 0
    for X in C2:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])
    cost_c2.append(cost)
    #3
    cost = 0
    for X in C3:
        cost += Dij[X-1][Y]
        # print(X-1,"->",Y,":",Dij[X-1][Y])    
    cost_c3.append(cost)

#1->2->3

def calc(c1,c2,c3):
    tmp1 = [c for c in c1]
    tmp2 = [c for c in c2]
    tmp3 = [c for c in c3]
    min1 = min(tmp1)
    arg1 = tmp1.index(min1)
    tmp2.remove(tmp2[arg1])
    tmp3.remove(tmp3[arg1])
    min2 = min(tmp2)
    arg2 = tmp2.index(min2)
    tmp3.remove(tmp3[arg2])
    min3 = min(tmp3)
    return min1+min2+min3
    
a1 = calc(cost_c1,cost_c2,cost_c3)
a2 = calc(cost_c1,cost_c3,cost_c2)
a3 = calc(cost_c2,cost_c1,cost_c3)
a4 = calc(cost_c2,cost_c3,cost_c1)
a5 = calc(cost_c3,cost_c1,cost_c2)
a6 = calc(cost_c3,cost_c2,cost_c1)

print(min(a1,a2,a3,a4,a5,a6))

Submission status: Wrong Answer

Solution code:
import sys

from collections import deque
# q = deque(); q.append(x); q.appendleft(x); q.pop() -> x; q.popleft() -> x
from collections import defaultdict
# d = defaultdict(lambda: []); d[key] -> []
import heapq
# hq = []; heapq.heappush(hq, x); heapq.heappop(hq) -> x; heapq.heappushpop(hq, x) -> x
import bisect
# i = bisect.bisect_left([1,2,3,4], 1.5); i = bisect.bisect_right([4,3,2,1], 1.5)

#import numpy as np
#from scipy.misc import comb

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

def main():
    N, C = i2nn()
    D = ii2nnn(C)
    c = ii2nnn(N)
    E = [[[0] * N for _ in range(N)] for _ in range(3)]
    for i in range(N):
        for j in range(N):
            c0 = c[i][j]
            E[0][i][j] = min(D[c0-1][k] for k in range(0, C, 3))
            E[1][i][j] = min(D[c0-1][k] for k in range(1, C, 3))
            E[2][i][j] = min(D[c0-1][k] for k in range(2, C, 3))
    F = [[0] * 3 for _ in range(3)]
    for i in range(N):
        for j in range(N):
            k = (i+j) % 3
            F[k][0] += E[0][i][j]
            F[k][1] += E[1][i][j]
            F[k][2] += E[2][i][j]
    #print(F)
    n = min(
        F[0][0] + F[1][1] + F[2][2],
        F[0][0] + F[1][2] + F[2][1],
        F[0][1] + F[1][2] + F[2][0],
        F[0][1] + F[1][0] + F[2][2],
        F[0][2] + F[1][1] + F[2][0],
        F[0][2] + F[1][0] + F[2][1],
    )
    print(n)

main()