void Solve(StreamScanner ss, StreamWriter sw) { //--------------------------------- var N = ss.Next(int.Parse); var C = ss.Next(int.Parse); var D = ss.Next(int.Parse, C, C); var cs = new int [3, C + 1]; for (var i = 0; i < N; i++) { for (var j = 0; j < N; j++) { cs[(i + j) % 3, ss.Next(int.Parse)]++; } } var ans = int.MaxValue; for (var i = 1; i <= C; i++) { for (var j = 1; j <= C; j++) { for (var k = 1; k <= C; k++) { if (i == j || j == k || k == i) { continue; } var total = 0; for (var c = 1; c <= C; c++) { total += cs[0, c] * D[c - 1][i - 1]; total += cs[1, c] * D[c - 1][j - 1]; total += cs[2, c] * D[c - 1][k - 1]; } ans = Math.Min(ans, total); } } } sw.WriteLine(ans); //--------------------------------- }