Problem: There is a grid with N rows and N columns of squares. Let (i,j) be the square at the i-th row from the top and the j-th column from the left.  These squares have to be painted in one of the C colors from Color 1 to Color C. Initially, (i,j) is painted in Color c_{i,j}.  We say the grid is a _good_ grid when the following condition is met for all i,j,x,y satisfying 1 \leq i,j,x,y \leq N:    * If (i+j) \% 3=(x+y) \% 3, the color of (i,j) and the color of (x,y) are the same.   * If (i+j) \% 3 \neq (x+y) \% 3, the color of (i,j) and the color of (x,y) are different.  Here, X \% Y represents X modulo Y.  We will repaint zero or more squares so that the grid will be a good grid.  For a square, the _wrongness_ when the color of the square is X before repainting and Y after repainting, is D_{X,Y}.  Find the minimum possible sum of the wrongness of all the squares.

Submission attempts (in order):
n,c = [int(i) for i in input().split()]
effort = []
group1 = []
group2 = []
group3 = []

effort1 = []
effort2 = []
effort3 = []

color1 = [0 for i in range(c)]
color2 = [0 for i in range(c)]
color3 = [0 for i in range(c)]

for i in range(c):
    effort.append(list(map(int,input().split())))

for i in range(n):
    k = 0
    for j in input().split():
        k+=1
        tot = i+1+k
        if tot % 3 == 0:
            group1.append(int(j))
        elif tot % 3 == 1:
            group2.append(int(j))
        elif tot % 3 == 2:
            group3.append(int(j))

for i in group1:
    color1[i-1] +=1
for i in group2:
    color2[i-1] +=1
for i in group3:
    color3[i - 1] += 1

print(color1,color2,color3)

for i in range(c):
    eff = 0
    for k in range(c):
        eff += color1[k] * effort[k][i]
    effort1.append([eff,i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color2[k] * effort[k][i]
    effort2.append([eff, i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color3[k] * effort[k][i]
    effort3.append([eff, i])

effort1 = sorted(effort1)
effort2 = sorted(effort2)
effort3 = sorted(effort3)
print(effort1,effort2,effort3)
toteff = 10000000000000000000
for i in range(3):
    col1 = effort1[i][1]
    for k in range(3):
        col2 = effort2[k][1]
        for j in range(3):
            col3 = effort3[j][1]
            if col1 != col2 and col2 !=col3 and col1!=col3:
                print(col1,col2,col3)
                print(effort1[i][0],effort2[k][0],effort3[j][0])
                toteff = min(toteff,effort1[i][0]+effort2[k][0]+effort3[j][0])

print(toteff)

n,c = [int(i) for i in input().split()]
effort = []
group1 = []
group2 = []
group3 = []

effort1 = []
effort2 = []
effort3 = []

color1 = [0 for i in range(c)]
color2 = [0 for i in range(c)]
color3 = [0 for i in range(c)]

for i in range(c):
    effort.append(list(map(int,input().split())))

for i in range(n):
    k = 0
    for j in input().split():
        k+=1
        tot = i+1+k
        if tot % 3 == 0:
            group1.append(int(j))
        elif tot % 3 == 1:
            group2.append(int(j))
        elif tot % 3 == 2:
            group3.append(int(j))

for i in group1:
    color1[i-1] +=1
for i in group2:
    color2[i-1] +=1
for i in group3:
    color3[i - 1] += 1

print(color1,color2,color3)

for i in range(c):
    eff = 0
    for k in range(c):
        eff += color1[k] * effort[k][i]
    effort1.append([eff,i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color2[k] * effort[k][i]
    effort2.append([eff, i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color3[k] * effort[k][i]
    effort3.append([eff, i])

effort1 = sorted(effort1)
effort2 = sorted(effort2)
effort3 = sorted(effort3)
print(effort1,effort2,effort3)
toteff = 10000000000000000000
for i in range(3):
    col1 = effort1[i][1]
    for k in range(3):
        col2 = effort2[k][1]
        for j in range(3):
            col3 = effort3[j][1]
            if col1 != col2 and col2 !=col3 and col1!=col3:
                print(col1,col2,col3)
                print(effort1[i][0],effort2[k][0],effort3[j][0])
                toteff = min(toteff,effort1[i][0]+effort2[k][0]+effort3[j][0])

print(toteff)

n,c = [int(i) for i in input().split()]
effort = []
group1 = []
group2 = []
group3 = []

effort1 = []
effort2 = []
effort3 = []

color1 = [0 for i in range(c)]
color2 = [0 for i in range(c)]
color3 = [0 for i in range(c)]

for i in range(c):
    effort.append(list(map(int,input().split())))

for i in range(n):
    k = 0
    for j in input().split():
        k+=1
        tot = i+1+k
        if tot % 3 == 0:
            group1.append(int(j))
        elif tot % 3 == 1:
            group2.append(int(j))
        elif tot % 3 == 2:
            group3.append(int(j))

for i in group1:
    color1[i-1] +=1
for i in group2:
    color2[i-1] +=1
for i in group3:
    color3[i - 1] += 1


for i in range(c):
    eff = 0
    for k in range(c):
        eff += color1[k] * effort[k][i]
    effort1.append([eff,i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color2[k] * effort[k][i]
    effort2.append([eff, i])
for i in range(c):
    eff = 0
    for k in range(c):
        eff += color3[k] * effort[k][i]
    effort3.append([eff, i])

effort1 = sorted(effort1)
effort2 = sorted(effort2)
effort3 = sorted(effort3)
toteff = 10000000000000000000
for i in range(3):
    col1 = effort1[i][1]
    for k in range(3):
        col2 = effort2[k][1]
        for j in range(3):
            col3 = effort3[j][1]
            if col1 != col2 and col2 !=col3 and col1!=col3:
                toteff = min(toteff,effort1[i][0]+effort2[k][0]+effort3[j][0])

print(toteff)

Verdicts: Wrong Answer

Wrong Answer

Accepted