using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static void Main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ List iMatrix = new List(9); for(int i = 0; i < 3; i++) { iMatrix.AddRange(Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse)); } MagicSquare sq = new MagicSquare(); Console.WriteLine(sq.cheapestSquareConv(iMatrix.ToArray())); } } class MagicSquare { int[][] sqArr = new int[][] { new int[] { 2, 7, 6, 9, 5, 1, 4, 3, 8 }, new int[] { 8, 1, 6, 3, 5, 7, 4, 9, 2 }, new int[] { 6, 1, 8, 7, 5, 3, 2, 9, 4 }, new int[] { 4, 9, 2, 3, 5, 7, 8, 1, 6 }, new int[] { 8, 3, 4, 1, 5, 9, 6, 7, 2 }, new int[] { 4, 3, 8, 9, 5, 1, 2, 7, 6 }, new int[] { 6, 7, 2, 1, 5, 9, 8, 3, 4 }, new int[] { 2, 9, 4, 7, 5, 3, 6, 1, 8 } }; int[] costs = new int[8]; public int cheapestSquareConv(int[] inMatrix) { int intSq = 0; foreach(int[] magic in sqArr) { for(int i = 0; i < 9; i++) { costs[intSq] += Math.Abs(magic[i] - inMatrix[i]); } intSq++; } int smallestCost = costs.Min(); return smallestCost; } }