<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 info {
public:
    int minSum;
    int NodeMax;
    info(int a, int b) {
        minSum = a;
        NodeMax = b;
    }
};
class Solution {
public:
    info solveBYRe(vector<int>& arr, int start, int end,
                   vector<vector<info>>& dp) {
        if (start > end) {
            info temp(0, 0);
            return temp;
        }
        if (start == end) {
            // we got leaf node
            // non leaf sum = 0
            info temp(0, arr[start]);
            return temp;
        }
        if (dp[start][end].minSum != -1) {
            return dp[start][end];
        }
        info ans(INT_MAX, 0);
        for (int i = start; i < end; i++) {
            info left = solveBYRe(arr, start, i, dp);
            info right = solveBYRe(arr, i + 1, end, dp);
            ans.NodeMax = max(left.NodeMax, right.NodeMax);
            int currentNode = left.NodeMax * right.NodeMax;
            ans.minSum =
                min(ans.minSum, currentNode + left.minSum + right.minSum);
        }
        dp[start][end].NodeMax = ans.NodeMax;
        dp[start][end].minSum = ans.minSum;
        return dp[start][end];
    }
    info solutionByTabulation(vector<int>& arr) {
        int n = arr.size();
        info temp(0, 0);
        vector<vector<info>> dp(n + 1, vector<info>(n + 1, temp));
        for (int i = 0; i < n; i++) {
            dp[i][i].NodeMax = arr[i];
        }
        for (int start = n - 1; start >= 0; start--) {
            for (int end = start + 1; end < n; end++) {
                info ans(INT_MAX, 0);
                for (int i = start; i < end; i++) {
                    info left = dp[start][i];
                    info right = dp[i+1][end];
                    ans.NodeMax = max(left.NodeMax, right.NodeMax);
                    int currentNode = left.NodeMax * right.NodeMax;
                    ans.minSum = min(ans.minSum,
                                     currentNode + left.minSum + right.minSum);
                }
                dp[start][end] = ans;
            }
        }
        return dp[0][n-1];
    }
    int mctFromLeafValues(vector<int>& arr) {
        int n = arr.size();
        info temp(-1, -1);
        vector<vector<info>> dp(n + 1, vector<info>(n + 1, temp));
        info ans = solutionByTabulation(arr);
        return ans.minSum;
    }
};
11900