<p>Given an array <code>arr</code> of positive integers, consider all binary trees such that:</p>

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

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

<p>A node is a <strong>leaf</strong> if and only if it has zero children.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/tree1.jpg" style="width: 500px; height: 169px;" />
<pre>
<strong>Input:</strong> arr = [6,2,4]
<strong>Output:</strong> 32
<strong>Explanation:</strong> There are two possible trees shown.
The first has a non-leaf node sum 36, and the second has non-leaf node sum 32.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/10/tree2.jpg" style="width: 224px; height: 145px;" />
<pre>
<strong>Input:</strong> arr = [4,11]
<strong>Output:</strong> 44
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= arr.length &lt;= 40</code></li>
	<li><code>1 &lt;= arr[i] &lt;= 15</code></li>
	<li>It is guaranteed that the answer fits into a <strong>32-bit</strong> signed integer (i.e., it is less than 2<sup>31</sup>).</li>
</ul>

class Solution {
public:
    int solveUsingRec(int s, int e, vector<vector<int>> &dp_max){
        // base case
        if( s == e) return 0;


        int ans = INT_MAX;
        
        for(int i = s; i<=e-1; i++){
            ans = min(ans, dp_max[s][i] * dp_max[i+1][e] + solveUsingRec(s, i, dp_max)+solveUsingRec(i+1, e, dp_max));
        }

        return ans;
    }
//--------------------------------------------------------------------------------------------------
 
    int solveUsingMem(int s, int e, vector<vector<int>> &dp_max, vector<vector<int>> &dp){
        // base case
        if( s == e) return 0;
        if(dp[s][e] != 0) return dp[s][e];

        int ans = INT_MAX;
        
        for(int i = s; i<=e-1; i++){
            ans = min(ans, dp_max[s][i] * dp_max[i+1][e] + solveUsingMem(s, i, dp_max, dp)+solveUsingMem(i+1, e, dp_max, dp));
        }
        
        dp[s][e] = ans;

        return ans;
    }

//---------------------------------------------------------------------------------------------------
    int solveUsingTab(int n, vector<vector<int>> &dp_max){
        vector<vector<int>>dp(n, vector<int>(n, 0));
        for(int i = n; i >= 0; i--){
            for(int j = i+1; j<= n-1; j++){
                int ans = INT_MAX;
                for(int it = i; it <= j-1; it++){
                    ans = min(ans, dp_max[i][it] * dp_max[it+1][j] + dp[i][it] + dp[it+1][j]);
                }
            dp[i][j] = ans;
            }
        }
    return dp[0][n-1];
    }
//---------------------------------------------------------------------------------------------------
    int mctFromLeafValues(vector<int>& arr) {
        int n = arr.size();
        vector<vector<int>>dp_max(n, vector<int>(n, 0));
        // storing max values of all possible ranges in dp_max
        for(int i = 0; i <= n-1; i++){
            dp_max[i][i] = arr[i];
            for(int j = i+1; j<=n-1; j++){
                dp_max[i][j] = max(arr[j], dp_max[i][j-1]);
            }
        }
        
        vector<vector<int>>dp(n, vector<int>(n, 0));
        int s = 0; 
        int e = n-1;

        // return solveUsingRec(s, e, dp_max);
        // return solveUsingMem(s, e, dp_max, dp);
        return solveUsingTab(n, dp_max);
    }
};
11800