text: One by one consider all elements as root and recursively find cost of the BST , compare the cost with min and update min if needed
code: for ( int r = i ; r <= j ; ++ r ) { int cost = optCost ( freq , i , r - 1 ) + optCost ( freq , r + 1 , j ) ; if ( cost < min ) min = cost ; }