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:
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(count)
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_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)

Submission status: Wrong Answer

Solution code:
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)])
#print(a)
#print(b)
p = [[],[],[]]
for i in range(3):
  p[i] = sorted(b[i],reverse = True)[: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))

Submission status: Wrong Answer

Solution code:
N, C = [int(s) for s in input().split()]

D = [0]
for i in range(C):
	D.append(list(map(lambda x: int(x), input().split())))
	D[i + 1].insert(0,0)

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

sumD = [[[1001, -1] for _ in range(3)] for _ in range(3)]

for i in range(1, C + 1):
	sum = [[0, i] for _ in range(3)]
	for j in range(N):
		for k in range(N):
			if (j + k) % 3 == 0:
				sum[0][0] += D[c[j][k]][i]
			elif (j + k) % 3 == 1:
				sum[1][0] += D[c[j][k]][i]
			else:
				sum[2][0] += D[c[j][k]][i]
	for j in range(3):
		if sumD[j][2][0] > sum[j][0]:
			sumD[j][2] = sum[j]
			sumD[j].sort()

ans = float('inf')

for i in range(3):
	for j in range(3):
		for k in range(3):
			if sumD[0][i][1] != sumD[1][j][1] and sumD[1][j][1] != sumD[2][k][1] and sumD[2][k][1] != sumD[0][i][1]:
				s = sumD[0][i][0] + sumD[1][j][0] + sumD[2][k][0]
				if s < ans:
					ans = s

print(ans)

Submission status: Wrong Answer

Solution code:
def sol():
    n,c=map(int,input().split())
    d=[[int(i) for i in input().split()] for i in range(c)]
    t=[[int(i) for i in input().split()] for i in range(n)]
    ans=-1
    for i in range(c**2):
        for k in range(c):
            i,j=i//c,i%c
            q=[i,j,k]
            if len(set(q))<3: continue
            chk=0
            for p in range(n**2):
                h,w=p//n,p%n
                x=(h+w+2)%3
                chk+=d[t[h][w]-1][q[x]]
            if ans==-1: ans=chk
            ans=min(ans,chk)

    print(ans)

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
def sol():
    n,c=map(int,input().split())
    d=[[int(i) for i in input().split()] for i in range(c)]
    t=[[int(i) for i in input().split()] for i in range(n)]
    ans=-1
    for i in range(c):
        for j in range(c):
            for k in range(c):
                q=[i,j,k]
                if len(set(q))<3: break
                chk=0
                for p in range(n**2):
                    h,w=p//n,p%n
                    x=(h+w+2)%3
                    chk+=d[t[h][w]-1][q[x]]
                if ans==-1: ans=chk
                ans=min(ans,chk)

    print(ans)

if __name__=="__main__":
    sol()

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
d=[[int(i) for i in input().split()] for i in range(c)]
t=[[int(i) for i in input().split()] for i in range(n)]
ans=-1
for i in range(c):
    for j in range(c):
        for k in range(c):
            q=[i,j,k]
            if len(set(q))<3: break
            chk=0
            for p in range(n**2):
                h,w=p//n,p%n
                x=(h+w)%3
                chk+=d[t[h][w]-1][q[x]]
            if ans==-1: ans=chk
            ans=min(ans,chk)

print(ans)

Submission status: Wrong Answer

Solution code:
# ContestName
# URL
import sys
#import numpy as np

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)
    def main2(cl):
        n = 0
        for i in range(N):
            for j in range(N):
                before = c[i][j]
                ref = (i + j + cl) % 3
                if before % 3 == ref:
                    #print('%d %d continue' % (i, j))
                    continue
                iwakan = 1000
                for after in range((ref+2)%3, C, 3):
                    d = D[before-1][after]
                    if d < iwakan:
                        iwakan = d
                n += iwakan
                #print('%d %d iwakan %d %d' % (i, j, iwakan, n))
        return n
    #print('step1')
    n1 = main2(1)
    #print('step2')
    n2 = main2(2)
    #print('step3')
    n3 = main2(3)
    n = min(n1, n2, n3)
    print(n)

main()

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
D=[]
r0=[]
r1=[]
r2=[]
for i in range(c):
    sub_list=list(map(int,input().split()))
    D.append(sub_list)
    r0.append(0)
    r1.append(0)
    r2.append(0)
c_list=[]
for i in range(n):
    sub_list=list(map(int,input().split()))
    c_list.append(sub_list)
for i in range(n):
    for j in range(n):
        if (i+j)%3==0:
            r0[c_list[i][j]-1]=r0[c_list[i][j]-1]+1
        elif (i+j)%3==1:
            r1[c_list[i][j]-1]=r1[c_list[i][j]-1]+1
        else:
            r2[c_list[i][j]-1]=r2[c_list[i][j]-1]+1
Min=10**10
for i in range(c):
    for j in range(i+1,c):
        for k in range(j+1,c):
            pat=[(i,j,k),(i,k,j),(j,i,k),(j,k,i),(k,i,j),(k,j,i)]
            for x in pat:
                Sum=0
                for i in range(c):
                    if i==x[0]:
                        Sum=Sum+r1[i]*D[i][x[1]]+r2[i]*D[i][x[2]]
                    elif i==pat[1]:
                        Sum=Sum+r0[i]*D[i][x[0]]+r2[i]*D[i][x[2]]
                    elif i==pat[2]:
                        Sum=Sum+r0[i]*D[i][x[0]]+r1[i]*D[i][x[1]]
                    else:
                        Sum=Sum+r0[i]*D[i][x[0]]+r1[i]*D[i][x[1]]+r2[i]*D[i][x[2]]
                if Sum<Min:
                    Min=Sum
print(Min)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
def main():
    N,C=map(int,raw_input().split())
    D=[None]
    for _ in range(C):
        D.append([None]+map(int,raw_input().split()))
    c=[]
    for _ in range(N):
        c.append(map(int,raw_input().split()))
    # F[C][mod]
    F=[[0,0,0] for _ in range(31)]
    # m[mod] = [c[mod]]
    m=[[] for i in range(3)]
    for i in range(N):
        for j in range(N):
            m[(i+j)%3].append(c[i][j])

    for mod in range(3):
        for color in range(1,C+1):
            val=0
            for mm in m[mod]:
                F[color][mod]+=D[mm][color]

    ans=1145141919810
    if N==1:
        for c0 in range(1,C+1):
            ans=min(ans,F[c0][0])
            print(ans)
            return

    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  
                ans=min(ans,F[c0][0]+F[c1][1]+F[c2][2])
    print(ans)





main()

Submission status: Wrong Answer

Solution code:
import itertools as it

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

d = []
for i in range(m):
    l = list(map(int,input().split()))
    d.append(l)

cs = [[] for i in range(3)]
for i in range(1,n+1):
    row = list(map(int,input().split()))
    for j, c in enumerate(row):
        cs[(i + (j+1)) % 3].append(c)

#print(cs)
ds = [[] for i in range(3)]
for i in range(1,m+1):
    for j in range(3):
        t = 0
        for e in cs[j]:
            t += d[e-1][i-1]
        ds[j].append((t,i))

#print(ds)
map(sorted, ds)

ans = -1
cands = list(it.product(ds[0][:3],ds[1][:3], ds[2][:3]))
for cand in cands:
#    print(cand)
    if cand[0][1] == cand[1][1] \
            or cand[1][1] == cand[2][1] \
            or cand[2][1] == cand[0][1]:
        continue
    calc = cand[0][0] + cand[1][0] + cand[2][0]
#    print(calc)
    if ans == -1:
        ans = calc
    else:
        ans = min(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
import itertools as it

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

d = []
for i in range(m):
    l = list(map(int,input().split()))
    d.append(l)

cs = [[] for i in range(3)]
for i in range(1,n+1):
    row = list(map(int,input().split()))
    for j, c in enumerate(row):
        cs[(i + (j+1)) % 3].append(c)

#print(cs)
ds = [[] for i in range(3)]
for i in range(1,m+1):
    for j in range(3):
        t = 0
        for e in cs[j]:
            t += d[e-1][i-1]
        ds[j].append((t,i))

#print(ds)

ans = -1
cands = list(it.product(ds[0][:3],ds[1][:3], ds[2][:3]))
for cand in cands:
#    print(cand)
    if cand[0][1] == cand[1][1] \
            or cand[1][1] == cand[2][1] \
            or cand[2][1] == cand[0][1]:
        continue
    calc = cand[0][0] + cand[1][0] + cand[2][0]
#    print(calc)
    if ans == -1:
        ans = calc
    else:
        ans = min(ans, calc)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

group0 = []
group1 = []
group2 = []
for i in range(N):
	for j in range(N):
		if (i+j)%3 == 0:
			group0.append(c[i][j]-1)
		elif (i+j)%3 == 1:
			group1.append(c[i][j]-1)
		elif (i+j)%3 == 2:
			group2.append(c[i][j]-1)
# print(group0)
# print(group1)
# print(group2)

color_0 = []
for i in range(C):
	tmp = 0
	for item0 in group0:
		tmp += D[item0][i]
	color_0.append((tmp, i))
color_0 = sorted(color_0)[::-1][0:3]
color_0_lis = [x[1] for x in color_0]

color_1 = []
for i in range(C):
	tmp = 0
	for item1 in group1:
		tmp += D[item1][i]
	color_1.append((tmp, i))
color_1 = sorted(color_1)[::-1][0:3]
color_1_lis = [x[1] for x in color_1]

color_2 = []
for i in range(C):
	tmp = 0
	for item2 in group2:
		tmp += D[item2][i]
	color_2.append((tmp, i))
color_2 = sorted(color_2)[::-1][0:3]
color_2_lis = [x[1] for x in color_2]
# print(color_0)
# print(color_1)
# print(color_2)
ans = INF
for i in color_0_lis:
	for j in color_1_lis:
		for k in color_2_lis:
			tmp = 0
			if i == j or j == k or k == i:
				continue
			else:
				for item0 in group0:
					tmp += D[item0][i]
				for item1 in group1:
					tmp += D[item1][j]
				for item2 in group2:
					tmp += D[item2][k]
				ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

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

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


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

b=[[0]*C for i in range(3)]
for i in range(3):
    for j in range(C):
        b[i][j]=sum([ D[k][j] * a[i][k] for k in range(C)])

b=[ sorted([ [b[i][j],j] for j in range(C)]) for i in range(3)]

ans=100000000
for i in range(3):
    for j in range(3):
        for k in range(3):
            if b[0][i][1]!=b[1][j][1] and b[1][j][1]!=b[2][k][1] and b[2][k][1]!=b[0][i][1]:
                ans=min(ans,b[0][i][0]+b[1][j][0]+b[2][k][0])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import itertools
N,C = map(int,input().split())
D = [list(map(int,input().split())) for i in range(C)]
grid = [list(map(int,input().split())) for i in range(N)]
color = list(range(1,C+1))
ans = 10**6
for x,y,z in itertools.permutations(color,3):
    res = 0
    for i in range(1,N+1):
        for j in range(1,N+1):
            if (i+j) % 3 == 0:
                res += D[grid[i-1][j-1]-1][x-1]
            elif (i+j) % 3 == 1:
                res += D[grid[i-1][j-1]-1][y-1]
            elif (i+j) % 3 == 2:
                res += D[grid[i-1][j-1]-1][z-1]
    if res < ans:
        ans = res
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools
N,C = map(int,input().split())
D = [list(map(int,input().split())) for i in range(C)]
grid = [list(map(int,input().split())) for i in range(N)]
color = list(range(1,C+1))
ans = float('inf')
for x,y,z in itertools.permutations(color,3):
    print(x,y,z)
    res = 0
    for i in range(1,N+1):
        for j in range(1,N+1):
            if (i+j) % 3 == 0:
                res += D[grid[i-1][j-1]-1][x-1]
            elif (i+j) % 3 == 1:
                res += D[grid[i-1][j-1]-1][y-1]
            elif (i+j) % 3 == 2:
                res += D[grid[i-1][j-1]-1][z-1]
    if res < ans:
        ans = res
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
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(C):
    for c1 in range(C):
        for c2 in range(C):
            if (c1-c0)*(c2-c1)*(c0-c2)==0: break
            iwa=0
            for x in range(N):
                for y in range(N):
                    if (x+y)%3==0: iwa+=D[G[x][y]-1][c2]
                    elif (x+y)%3==1: iwa+=D[G[x][y]-1][c0]
                    elif (x+y)%3==2: iwa+=D[G[x][y]-1][c1]
            miniwa=min(miniwa,iwa)
print(miniwa)

Submission status: Wrong Answer

Solution code:
N, C = [int(x) for x in input().split()]
D = [[0] * C for _ in range(C)]
c = [[0] * N for _ in range(N)]

for i in range(C):
    D[i] = [int(x) for x in input().split()]
for i in range(N):
    c[i] = [int(x) for x in input().split()]

I = [[0, 0, 0] for _ in range(C)]


for i in range(N):
    for j in range(N):
        pattern = (i + j) % 3
        I[0][pattern] += D[c[i][j] - 1][0]
        I[1][pattern] += D[c[i][j] - 1][1]
        I[2][pattern] += D[c[i][j] - 1][2]


ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
import itertools
#横にn個入力
n, c = map(int, input().split())
#行列
d = []
for i in range(c):#h:高さ
    d.append([int(m) for m in input().split()])
#行列
e = []
for i in range(n):#h:高さ
    e.append([int(m) for m in input().split()])

li0 = []
li1 = []
li2 = []
for i in range(n):
    for j in range(n):
        if (i + j + 2) % 3 == 0:
            li0.append(e[i][j])
        elif (i + j + 2) % 3 == 1:
            li1.append(e[i][j])
        elif (i + j + 2) % 3 == 2:
            li2.append(e[i][j])

c0 = collections.Counter(li0)
c1 = collections.Counter(li1)
c2 = collections.Counter(li2)

cost0 = {}
cost1 = {}
cost2 = {}
dost0 = []
dost1 = []
dost2 = []
k0 = 0
k1 = 0
k2 = 0
for i in range(c):
    for v0 in c0:
        k0 += d[v0 - 1][i] * c0[v0]
    cost0[i + 1] = k0
    dost0.append([k0, i + 1])
    k0 = 0
    for v1 in c1:
        k1 += d[v1 - 1][i] * c1[v1]
    cost1[i + 1] = k1
    dost1.append([k1, i + 1])
    k1 = 0
    for v2 in c2:
        k2 += d[v2 - 1][i] * c2[v2]
    cost2[i + 1] = k2
    dost2.append([k2, i + 1])
    k2 = 0

dost0.sort(reverse=True)
dost1.sort(reverse=True)
dost2.sort(reverse=True)
w0 = [dost0[0][1], dost0[1][1], dost0[2][1]]
w1 = [dost1[0][1], dost1[1][1], dost1[2][1]]
w2 = [dost2[0][1], dost2[1][1], dost2[2][1]]

ansli = []
for x0 in itertools.permutations(w0, 1):
    for x1 in itertools.permutations(w1, 1):
        for x2 in itertools.permutations(w2, 1):
            if x0[0] != x1[0] and x1[0] != x2[0] and x2[0] != x0[0]:
                ansli.append(cost0[x0[0]] + cost1[x1[0]] + cost2[x2[0]])

print(min(ansli))

Submission status: Wrong Answer

Solution code:
import collections
import itertools
#横にn個入力
n, c = map(int, input().split())
#行列
d = []
for i in range(c):#h:高さ
    d.append([int(m) for m in input().split()])
#行列
e = []
for i in range(n):#h:高さ
    e.append([int(m) for m in input().split()])

li0 = []
li1 = []
li2 = []
for i in range(n):
    for j in range(n):
        if (i + j + 2) % 3 == 0:
            li0.append(e[i][j])
        elif (i + j + 2) % 3 == 1:
            li1.append(e[i][j])
        elif (i + j + 2) % 3 == 2:
            li2.append(e[i][j])

c0 = collections.Counter(li0)
c1 = collections.Counter(li1)
c2 = collections.Counter(li2)

cost0 = {}
cost1 = {}
cost2 = {}
dost0 = []
dost1 = []
dost2 = []
k0 = 0
k1 = 0
k2 = 0
for i in range(c):
    for v0 in c0:
        k0 += d[v0 - 1][i] * c0[v0]
    cost0[i + 1] = k0
    dost0.append([k0, i + 1])
    k0 = 0
    for v1 in c1:
        k1 += d[v1 - 1][i] * c1[v1]
    cost1[i + 1] = k1
    dost1.append([k1, i + 1])
    k1 = 0
    for v2 in c2:
        k2 += d[v2 - 1][i] * c2[v2]
    cost2[i + 1] = k2
    dost2.append([k2, i + 1])
    k2 = 0

dost0.sort(reverse=True)
dost1.sort(reverse=True)
dost2.sort(reverse=True)
w0 = [dost0[0][1], dost0[1][1], dost0[2][1]]
w1 = [dost1[0][1], dost1[1][1], dost1[2][1]]
w2 = [dost2[0][1], dost2[1][1], dost2[2][1]]

ansli = []
for x0 in itertools.permutations(w0, 1):
    for x1 in itertools.permutations(w1, 1):
        for x2 in itertools.permutations(w2, 1):
            if x0 != x1 and x1 != x2 and x2 != x0:
                ansli.append(cost0[x0[0]] + cost1[x1[0]] + cost2[x2[0]])

print(min(ansli))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(c)]

cnt = {
    0: {},
    1: {},
    2: {}
}

for i in range(n):
    cl = list(map(int, input().split()))
    for j in range(n):
        k = (i+j)%3
        if cl[j] not in cnt[k]:
                cnt[k][cl[j]] = 0
        cnt[k][cl[j]] += 1

res = 10**3 * 500*2
for ci in range(c):
    for cj in range(c):
        if ci==cj:
            continue
        for ck in range(c):
            if ci==ck or cj==ck:
                continue

            t = 0
            cc = [ci, cj, ck]
            for r in range(3):
                for k in cnt[r].keys():
                    t += d[k-1][cc[r]]*cnt[r][k]
            res = min(res, t)
print(res)

Submission status: Wrong Answer

Solution code:
import itertools

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

costs = [[float('inf')]*3 for i in range(C)]
for fr in range(C):
    for to in range(C):
        costs[fr][to%3] = min(costs[fr][to%3], D[fr][to])

ans = float('inf')
for i,ptn in enumerate(itertools.permutations(range(3))):
    cost = 0
    for y in range(N):
        for x in range(N):
            to = ptn[(x+y)%3]
            c = src[y][x] - 1
            cost += costs[c][to]
    ans = min(ans, cost)
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

def ans(list1,list2,list3):
    l1 = copy.deepcopy(list1)
    l2 = copy.deepcopy(list2)
    l3 = copy.deepcopy(list3)
    aa = min(l1)
    bb = l1.index(aa)
    l2[bb] = 1000
    l3[bb] = 1000
    cc = min(l2)
    dd = l2.index(cc)
    l3[dd] = 1000
    ee = min(l3)
    return aa+cc+ee

N,C = map(int,input().split(" "))
D = []
c0 = []
c1 = []
c2 = []

for i in range(C):
    aa = list(map(int,input().split(" ")))
    D.append(aa)
for i in range(N):
    aa = list(map(int,input().split(" ")))
    for j in range(N):
        if (i+j)%3 == 0:
            c0.append(aa[j])
        elif (i+j)%3 == 1:
            c1.append(aa[j])
        else:
            c2.append(aa[j])
c0cost = []
c1cost = []
c2cost = []
for i in range(C):
    cost = 0
    for j in range(len(c0)):
        cost += D[c0[j]-1][i]
    c0cost.append(cost)
for i in range(C):
    cost = 0
    for j in range(len(c1)):
        cost += D[c1[j]-1][i]
    c1cost.append(cost)
for i in range(C):
    cost = 0
    for j in range(len(c2)):
        cost += D[c2[j]-1][i]
    c2cost.append(cost)
print(min(ans(c0cost,c1cost,c2cost),ans(c0cost,c2cost,c1cost),ans(c1cost,c0cost,c2cost),ans(c1cost,c2cost,c0cost),ans(c2cost,c0cost,c1cost),ans(c2cost,c1cost,c0cost)))

Submission status: Wrong Answer

Solution code:
n,c = [int(i) for i in input().split()]
d = [] 
for i in range(c):
    d.append(list(map(int, input().split())))
color = []
for i in range(n):
    color.append(list(map(int, input().split())))
for i in range(n):
    for j in range(n):
        color[i][j] -= 1
dofc = [[0 for i in range(3)] for j in range(c)]
for t in range(3):
    for a in range(c):
        for l in range(n):
            for row in range(n):
                dofc[a][(l+row)%3] += d[a][color[row][l]]
D = dofc[0][0]+dofc[1][1]+dofc[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,dofc[c0][0]+dofc[c1][1]+dofc[c2][2])
print(D)

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
D = [[int(i) for i in input().split()] for _ in range(C)]
c = [[int(i) for i in input().split()] for _ 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
I = [x[0] for x in sorted([[i,v] for [i,v] in enumerate(D_sum[0])],reverse=True,key=lambda x:x[1])][:3]
J = [x[0] for x in sorted([[i,v] for [i,v] in enumerate(D_sum[1])],reverse=True,key=lambda x:x[1])][:3]
K = [x[0] for x in sorted([[i,v] for [i,v] in enumerate(D_sum[2])],reverse=True,key=lambda x:x[1])][:3]
for i in I:
    for j in J:
        for k in K:
            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)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, C = [int(i) for i in input().split(' ')]

dist = []

for _ in range(C):
    tmp = [int(x) for x in input().split(' ')]
    dist.append(tmp)

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

for i in range(1, N+1):
    for j, x in enumerate(input().split(' ')):
        j = j+1
        x= int(x)-1

        color_map[(i+j)%3][x] += 1

cost = 99999999

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_cost = 0
            for i in range(C):
                tmp_cost += dist[i][c1] * color_map[0][i]
                tmp_cost += dist[i][c2] * color_map[1][i]
                tmp_cost += dist[i][c3] * color_map[2][i]
            
            cost = min(cost, tmp_cost)
if cost == 99999999:
    cost = 0
print(cost)

Submission status: Wrong Answer

Solution code:
N, C = [int(i) for i in input().split(' ')]

dist = []

for _ in range(C):
    tmp = [int(x) for x in input().split(' ')]
    dist.append(tmp)

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

for i in range(1, N+1):
    for j, x in enumerate(input().split(' ')):
        j = j+1
        x= int(x)-1

        color_map[(i+j)%3][x] += 1

cost = 99999999

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_cost = 0
            for i in range(C):
                tmp_cost += dist[i][c1] * color_map[0][i]
                tmp_cost += dist[i][c2] * color_map[1][i]
                tmp_cost += dist[i][c3] * color_map[2][i]
            
            cost = min(cost, tmp_cost)
print(cost)

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)

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)