import java.util.*;


class Ideone{

	public static void main(String[] args){
		Box[] boxArray = getInitialBoxDimensions();
		Arrays.sort(boxArray);
		System.out.println("Max height => " + longestHeightIncreasingSubseq(boxArray));
	}

	private static int longestHeightIncreasingSubseq(Box[] boxes){
		int[] dp = new int[boxes.length];
		for(int i=0; i<dp.length; i++){
			dp[i] = boxes[i].height;
		}


	    for(int i=1; i<boxes.length; i++){
			for(int j=0; j<i; j++){
				if(dp[i] < dp[j]+boxes[i].height && boxes[i].width < boxes[j].width && boxes[i].length < boxes[j].length){
					dp[i] = dp[j] + boxes[i].height; 
				}				
			}
		}

		for(int i=0; i<boxes.length; i++){
			System.out.println("lis =>" + dp[i] + " height => "+boxes[i].height + " area => "+ (boxes[i].width * boxes[i].length));
		}	

		//Iterate over dp array and print max value
		int maxHeight = 0;
		for(int i=0; i<dp.length; i++){
			maxHeight = max(dp[i], maxHeight);
		}

		return maxHeight;
	}

	private static Box[] getInitialBoxDimensions(){
		Box[] array1 = getPossibleOrientations(4, 6, 7);
		Box[] array2 = getPossibleOrientations(1,2,3);
		Box[] array3 = getPossibleOrientations(4,5,6);
		Box[] array4 = getPossibleOrientations(10,12,32);

		Box[] result = mergeTwoArrays(array1, array2);
        result = mergeTwoArrays(result, array3);	
        result = mergeTwoArrays(result, array4);

		return result;	
	}

	private static Box[] mergeTwoArrays(Box[] a1, Box[] a2){
		Box[] result = new Box[a1.length + a2.length];
		int tracker =0;
		for(int i=0; i<a1.length; i++){
			result[tracker++] = a1[i];
		}

		for(int j=0;j<a2.length; j++){
			result[tracker++] = a2[j];
		}
		return result;
	}

	private static Box[] getPossibleOrientations(int x, int y, int z){
		Box b1 = new Box(x,y,z);
		Box b2 = new Box(x,z,y);
		Box b3 = new Box(y,z,x);
		Box b4 = new Box(y,x,z);
		Box b5 = new Box(z,x,y);
		Box b6 = new Box(z,y,x);
		Box[]  array = {b1, b2, b3, b4, b5, b6};
		
		return array;			
	}

	private static int min(int a, int b){
		return a<b ? a : b; 
	}

	private static int max(int a, int b){
		return a > b ? a :b;
	}

	private static class Box implements Comparable<Box>{
		int width;
		int length;//depth
		int height;

		Box(int height, int length, int width){
			this.width = width;
			this.length = length;
			this.height = height;
		}

		@Override
	    public int compareTo(Box that){
			int areaOfThisObj = this.width * this.length;
			int areaOfThatObj = that.width * that.length;
			return areaOfThatObj - areaOfThisObj;
		}
	}
}
