import java.util.*; public class JohnTwentyFour { public static final int MAXSIZE =8; public static final char LEAF = 'L'; public static final int PARENCOST = 1; public static final int INVCOST = 2; public static final int IMPOSSIBLE = 1000000; public static Scanner in = new Scanner(System.in); public static int [] original = new int[MAXSIZE]; public static int countParens(node tree) { if (tree.op == LEAF) return 0; else { int count = countParens(tree.left) + countParens(tree.right); if (tree.op == '-' && tree.right.op == '-') count++; else if (tree.op == '*' || tree.op == '/') { if (tree.left.op == '+' || tree.left.op == '-') count++; if (tree.right.op == '+' || tree.right.op == '-') count++; if (tree.op == '/' && tree.right.op == '/') count++; } return count; } } public static int numParens(node tree) { int count = countParens(tree); return count; } public static int find24(node [] tlist, int numTerms) { if (numTerms == 1) { if (tlist[0].val == 24) return numParens(tlist[0]); else return IMPOSSIBLE; } else { int cost = IMPOSSIBLE; for(int i=1; i= i; k--) buildList[k+1] = buildList[k]; buildList[i] = tmp; } return count; } public static int permute(int [] list, int index, int n) { if (index == n) { int numInv = numInversions(list, n); node [] tlist = new node[n]; for(int i=0; i= IMPOSSIBLE) System.out.println("impossible"); else System.out.println(ans); } } class node { public int val; public char op; public node left, right; }