import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int m = 3; int n = 3; int arr[][] = new int[m][n]; List magicSquare = new ArrayList(); for(int i=0; i sq1 = new ArrayList(); List sq2 = new ArrayList(); List sq3 = new ArrayList(); List sq4 = new ArrayList(); List sq5 = new ArrayList(); List sq6 = new ArrayList(); List sq7 = new ArrayList(); List sq8 = new ArrayList(); //Square 1! sq1.addAll(Arrays.asList(elem1, elem2, elem3, elem4, elem5, elem6, elem7, elem8, elem9)); // System.out.println(elem1 + " " + elem2 + " " + elem3 + "\n" + // elem4 + " " + elem5 + " " + elem6 + "\n" + // elem7 + " " + elem8 + " " + elem9 + "\n"); //Square 2! sq2.addAll(Arrays.asList(elem3, elem6, elem9, elem2, elem5, elem8, elem1, elem4, elem7)); // System.out.println(elem3 + " " + elem6 + " " + elem9 + "\n" + // elem2 + " " + elem5 + " " + elem8 + "\n" + // elem1 + " " + elem4 + " " + elem7 + "\n"); //Square 3! sq3.addAll(Arrays.asList(elem7, elem4, elem1, elem8, elem5, elem2, elem9, elem6, elem3)); // System.out.println(elem7 + " " + elem4 + " " + elem1 + "\n" + // elem8 + " " + elem5 + " " + elem2 + "\n" + // elem9 + " " + elem6 + " " + elem3 + "\n"); //Square 4! sq4.addAll(Arrays.asList(elem9, elem8, elem7, elem6, elem5, elem4, elem3, elem2, elem1)); // System.out.println(elem9 + " " + elem8 + " " + elem7 + "\n" + // elem6 + " " + elem5 + " " + elem4 + "\n" + // elem3 + " " + elem2 + " " + elem1 + "\n"); //Square 5! sq5.addAll(Arrays.asList(elem1, elem4, elem7, elem2, elem5, elem8, elem3, elem6, elem9)); // System.out.println(elem1 + " " + elem4 + " " + elem7 + "\n" + // elem2 + " " + elem5 + " " + elem8 + "\n" + // elem3 + " " + elem6 + " " + elem9 + "\n"); //Square 6! sq6.addAll(Arrays.asList(elem3, elem2, elem1, elem6, elem5, elem4, elem9, elem8, elem7)); // System.out.println(elem3 + " " + elem2 + " " + elem1 + "\n" + // elem6 + " " + elem5 + " " + elem4 + "\n" + // elem9 + " " + elem8 + " " + elem7 + "\n"); //Square 7! sq7.addAll(Arrays.asList(elem7, elem8, elem9, elem4, elem5, elem6, elem1, elem2, elem3)); // System.out.println(elem7 + " " + elem8 + " " + elem9 + "\n" + // elem4 + " " + elem5 + " " + elem6 + "\n" + // elem1 + " " + elem2 + " " + elem3 + "\n"); //Square 8! sq8.addAll(Arrays.asList(elem9, elem6, elem3, elem8, elem5, elem2, elem7, elem4, elem1)); // System.out.println(elem9 + " " + elem6 + " " + elem3 + "\n" + // elem8 + " " + elem5 + " " + elem2 + "\n" + // elem7 + " " + elem4 + " " + elem1 + "\n"); // /*System.out.println(sq1); System.out.println(sq2); System.out.println(sq3); System.out.println(sq4); System.out.println(sq5); System.out.println(sq6);*/ //squares int total = 0; int minCost = 0; int temp1 = 0; int diff1 = 0; int temp2 = 0; int diff2 = 0; int temp3 = 0; int diff3 = 0; int temp4 = 0; int diff4 = 0; int temp5 = 0; int diff5 = 0; int temp6 = 0; int diff6 = 0; int temp7 = 0; int diff7 = 0; int temp8 = 0; int diff8 = 0; List differences = new ArrayList(); for(int j=0; j<9; j++){ if(magicSquare.get(j) != sq1.get(j)){ temp1 = Math.abs(sq1.get(j) - magicSquare.get(j)); diff1 += temp1; // System.out.println("My Square [i]: " + magicSquare.get(j)); // System.out.println("Magic Square 1[i]: " + sq1.get(j)); // System.out.println("Difference: " + (Math.abs(sq1.get(j) - magicSquare.get(j)))/* + "\n"*/); // System.out.println("Calc Difference: " + difference + "\n"); } if(magicSquare.get(j) != sq2.get(j)){ temp2 = Math.abs(sq2.get(j) - magicSquare.get(j)); diff2 += temp2; } if(magicSquare.get(j) != sq3.get(j)){ temp3 = Math.abs(sq3.get(j) - magicSquare.get(j)); diff3 += temp3; } if(magicSquare.get(j) != sq4.get(j)){ temp4 = Math.abs(sq4.get(j) - magicSquare.get(j)); diff4 += temp4; } if(magicSquare.get(j) != sq5.get(j)){ temp5 = Math.abs(sq5.get(j) - magicSquare.get(j)); diff5 += temp5; } if(magicSquare.get(j) != sq6.get(j)){ temp6 = Math.abs(sq6.get(j) - magicSquare.get(j)); diff6 += temp6; } if(magicSquare.get(j) != sq7.get(j)){ temp7 = Math.abs(sq7.get(j) - magicSquare.get(j)); diff7 += temp7; } if(magicSquare.get(j) != sq8.get(j)){ temp8 = Math.abs(sq8.get(j) - magicSquare.get(j)); diff8 += temp8; } } // System.out.println(diff1); // System.out.println(diff1); differences.addAll(Arrays.asList(diff1, diff2, diff3, diff4, diff5, diff6, diff7, diff8)); //System.out.println(differences); Collections.sort(differences); // Sort the arraylist System.out.println(differences.get(differences.size() - 8)); in.close(); } }