/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
int[][] land = {{1,2,3,5},{5,6,7,8},{4,3,2,1}};
ArrayList<ArrayList<Integer>> arrList = new ArrayList<ArrayList<Integer>>();
getCase(land, new boolean[land[0].length], new ArrayList<Integer>(), arrList,0);
int max = 0;
for(int i = 0 ; i < arrList.size() ; i++ ){
int sum = 0;
for(int j = 0 ; j < arrList.get(i).size() ; j++){
sum += arrList.get(i).get(j);
}
if(max < sum){
max = sum;
}
}
// your code goes here
}
private static void getCase(int[][] land, boolean[] check, ArrayList<Integer> arr,ArrayList<ArrayList<Integer>> arrList,int index){
if(arr.size() == land.length){
arrList.add(new ArrayList<>(arr));
return ;
}
for(int i = index ; i < land.length ; i++){
for(int j = 0 ; j < land[i].length ; j++){
if(!check[j]){
arr.add(land[i][j]);
check[j] = true;
getCase(land, check, arr, arrList,i+1);
arr.remove(arr.size() -1);
check[j] = false;
}
}
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aHJvd3MgamF2YS5sYW5nLkV4Y2VwdGlvbgoJewoJCWludFtdW10gbGFuZCA9IHt7MSwyLDMsNX0sezUsNiw3LDh9LHs0LDMsMiwxfX07CgkJQXJyYXlMaXN0PEFycmF5TGlzdDxJbnRlZ2VyPj4gYXJyTGlzdCA9IG5ldyBBcnJheUxpc3Q8QXJyYXlMaXN0PEludGVnZXI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