Given an array arr of positive integers, consider all binary trees such that:


	Each node has either 0 or 2 children;
	The values of arr correspond to the values of each leaf in an in-order traversal of the tree.
	The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree, respectively.


Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node. It is guaranteed this sum fits into a 32-bit integer.

A node is a leaf if and only if it has zero children.

&nbsp;
Example 1:

Input: arr = [6,2,4]
Output: 32
Explanation: There are two possible trees shown.
The first has a non-leaf node sum 36, and the second has non-leaf node sum 32.


Example 2:

Input: arr = [4,11]
Output: 44


&nbsp;
Constraints:


	2 &lt;= arr.length &lt;= 40
	1 &lt;= arr[i] &lt;= 15
	It is guaranteed that the answer fits into a 32-bit signed integer (i.e., it is less than 231).


class Solution:
    def mctFromLeafValues(self, arr: List[int]) -> int:
        n = len(arr)
        d = {}
        def findMax(start,end):
            if (start,end) in d: return d[(start,end)]
            maxx = start
            for i in range(start+1,end+1):
                if arr[maxx] < arr[i] : maxx = i
            d[(start,end)] = arr[maxx]
            return arr[maxx]

        dp = [[float('inf') for i in range(n)] for j in range(n)]
        for gap in range(n):
            for row in range(n - gap):
                col = row + gap
                if gap == 0:
                    dp[row][col] = 0
                elif gap == 1:
                    dp[row][col] = arr[row] * arr[col]
                else:
                    for k in range(row,col):
                        val = dp[row][k] + findMax(row,k) * findMax(k+1,col) + dp[k+1][col]
                        if val < dp[row][col]: dp[row][col] = val

        return dp[0][-1]
class Solution {
    public int mctFromLeafValues(int[] arr) {
        
        int sum = 0;
        Stack<Integer> stack = new Stack<>();
        for (int num: arr) {
            sum += cleanUpStack(num, stack);
        }
        sum += cleanUpStack(17, stack);
        
        return sum;
    }
    
    private int cleanUpStack(int target, Stack<Integer> stack) {
        int last = 0;
        int sum = 0;
        while (!stack.isEmpty() && stack.peek() <= target) {
            int cur = stack.pop();
            sum += last * cur;
            last = cur;
        }
        if (target != 17) {
            sum += target * last;
            stack.push(target);
        }
        return sum;
    }
}
class Solution {
public:
    int dp[40][40]={0};
    int solve(vector<int> arr,int start,int end)
    {
        if(start==end)
            return 0;
        if(dp[start][end]!=0)
            return dp[start][end];
        int mn=INT_MAX;
        for(int i=start;i<=end-1;i++)
        {
            int left=solve(arr,start,i);
            int right=solve(arr,i+1,end);
            int temp=left+right+*max_element(arr.begin()+start,arr.begin()+i+1) * *max_element(arr.begin()+i+1,arr.begin()+end+1);
            mn=min(mn,temp);
            
        }
        return dp[start][end]=mn;
    }
    int mctFromLeafValues(vector<int>& arr)
    {
        return solve(arr,0,arr.size()-1);
    }
};
//if you like the solution plz upvote.
var mctFromLeafValues = function(arr) {
  const dp = [];

  for (let i = 0; i < arr.length; i++) {
    dp[i] = [];
  }

  return treeBuilder(0, arr.length - 1);

  function treeBuilder(start, end) {

    if (start == end) {
      return 0;
    }

    if (dp[start][end]) {
      return dp[start][end];
    }

    let min = Number.MAX_VALUE;

    for (let i = start; i < end; i++) {
      const left = treeBuilder(start, i);
      const right = treeBuilder(i + 1, end);

      const maxLeft = Math.max(...arr.slice(start, i + 1));
      const maxRight = Math.max(...arr.slice(i + 1, end + 1));

      const rootVal = maxLeft * maxRight;

      min = Math.min(min, rootVal + left + right);

    }

    dp[start][end] = min;
    return min;
  }
};
Minimum Cost Tree From Leaf Values