Optimal binary search tree
Your task
Given a sorted list of distinct keys with access frequencies 'freq' (freq[i] is how often key i is searched), build a binary search tree that minimizes the total expected search cost. The cost of an arrangement is the sum over all keys of freq[key] * (depth-of-key, counting the root as depth 1). Return the minimum possible total cost. 1 <= len(freq) <= 250, 0 <= freq[i] <= 10^5.
Examples
optimal_bst(freq: list[int]) -> intExample 1
Input
optimal_bst([34, 8, 50])Output
142