object Solution { def main(args: Array[String]) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ val magicSqrOfOrder: Int = 3 val magicConstants: Int = 15 var cost: Int = 0 val rcType1:Array[Array[Int]] = Array(Array(4,9,2), Array(3,5,7), Array(8,1,6)) val rcType2:Array[Array[Int]] = Array(Array(2,7,6), Array(9,5,1), Array(4,3,8)) val rcType3:Array[Array[Int]] = Array(Array(6,1,8), Array(7,5,3), Array(2,9,4)) val rcType4:Array[Array[Int]] = Array(Array(8,3,4), Array(1,5,9), Array(6,7,2)) val rcType5:Array[Array[Int]] = Array(Array(8,1,6), Array(3,5,7), Array(4,9,2)) val rcType6:Array[Array[Int]] = Array(Array(4,3,8), Array(9,5,1), Array(2,7,6)) val rcType7:Array[Array[Int]] = Array(Array(2,9,4), Array(7,5,3), Array(6,1,8)) val rcType8:Array[Array[Int]] = Array(Array(6,7,2), Array(1,5,9), Array(8,3,4)) var rc:Array[Array[Int]] = Array.ofDim[Int](magicSqrOfOrder, magicSqrOfOrder) rc(0) = readLine().split(" ").map(_.toInt) rc(1) = readLine().split(" ").map(_.toInt) rc(2) = readLine().split(" ").map(_.toInt) def checkCost(rc:Array[Array[Int]], rcType:Array[Array[Int]]): Int = { var tmpCost = 0 for(r <- 0 until magicSqrOfOrder){ for(c <- 0 until magicSqrOfOrder){ tmpCost += Math.abs(rc(r)(c) - rcType(r)(c)) } } tmpCost } val listOfCost: List[Int] = List(checkCost(rc, rcType1), checkCost(rc, rcType2), checkCost(rc, rcType3), checkCost(rc, rcType4), checkCost(rc, rcType5), checkCost(rc, rcType6), checkCost(rc, rcType7), checkCost(rc, rcType8)).sorted println(listOfCost(0)) } }