Optimal binary search tree

Write codeHard35 min

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]) -> int
Example 1
Inputoptimal_bst([34, 8, 50])
Output142