human
Optimal Binary Search Tree | DP | A utility function to get sum of array elements freq [ i ] to freq [ j ].
A recursive function to calculate cost of optimal binary search tree.
Base cases no elements in this subarray.
one element in this subarray.
Get sum of freq [ i ] , freq [ i + 1 ] , ... freq [ j ].
Initialize minimum value.
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.
Return minimum value.
The main function that calculates minimum cost of a Binary Search Tree . It mainly uses optCost ( ) to find the optimal cost ..
Here array keys [ ] is assumed to be sorted in increasing order . If keys [ ] is not sorted , then add code to sort keys , and rearrange freq [ ] accordingly ..
Driver Code"
Solution in Python:

=====
Give me a code to optimal Binary Search Tree and the other steps required?
assistant
#Here is code to optimal Binary Search Tree and the other steps required.
 def Sum ( freq , i , j ) :
	s = 0
	for k in range ( i , j + 1 ) :
		s += freq [ k ]
	return s
def optCost ( freq , i , j ) :
	if j < i :
		return 0
	if j == i :
		return freq [ i ]
	fsum = Sum ( freq , i , j )
	Min = 999999999999
	for r in range ( i , j + 1 ) :
		cost = ( optCost ( freq , i , r - 1 ) + optCost ( freq , r + 1 , j ) )
		if cost < Min :
			Min = cost
	return Min + fsum
def optimalSearchTree ( keys , freq , n ) :
	return optCost ( freq , 0 , n - 1 )
if __name__ == ' _ _ main _ _ ' :
	keys = [ 10 , 12 , 20 ]
	freq = [ 34 , 8 , 50 ]
	n = len ( keys )
	print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) )