class Solution:
    def minCost(self, n: int, cuts: List[int]) -> int:
        cuts = [0] + sorted(cuts) + [n]
        k = len(cuts)
        dp = [[float('inf')] * k for _ in range(k)]
        for l in range(1, k + 1):
            for beg in range(k - l):
                end = beg + l
                if l == 1:
                    dp[beg][end] = 0
                    continue
                for i in range(beg + 1, end):
                    currcost = cuts[end] - cuts[beg]
                    currcost += dp[beg][i] + dp[i][end]
                    dp[beg][end] = min(dp[beg][end], currcost)
        return dp[0][k - 1]
class Solution {
    public int minCost(int n, int[] cuts) {
        
        int len = cuts.length;
        
        Arrays.sort(cuts);
        
        int[] arr = new int[len+2];
        for(int i = 1 ; i <= len ; i++)
            arr[i] = cuts[i-1];
        
        arr[0] = 0;
        arr[len+1] = n;
        int[][] dp = new int[len+1][len+1];
        for(int i = 0 ; i <= len ; i++)
            for(int j = 0 ; j <= len ; j++)
                dp[i][j] = -1;
        return cut(arr , 1 , len , dp);
    }
    
    int cut(int[] cuts , int i , int j , int[][] dp){
        if(i > j)
            return 0;
        
        if(dp[i][j] != -1)
            return dp[i][j];
        
        int mini = Integer.MAX_VALUE;
        for(int k = i ; k <= j ; k++){
            int cost = cuts[j+1]-cuts[i-1] + cut(cuts , i , k-1 , dp) + cut(cuts , k+1 , j , dp);
            mini = Math.min(cost , mini);
        }
        
        return dp[i][j] = mini;
    }
}
Minimum Cost to Cut a Stick
Given a wooden stick of length n units. The stick is labelled from 0 to n. For example, a stick of length 6 is labelled as follows:

Given an integer array cuts where cuts[i] denotes a position you should perform a cut at.

You should perform the cuts in order, you can change the order of the cuts as you wish.

The cost of one cut is the length of the stick to be cut, the total cost is the sum of costs of all cuts. When you cut a stick, it will be split into two smaller sticks (i.e. the sum of their lengths is the length of the stick before the cut). Please refer to the first example for a better explanation.

Return the minimum total cost of the cuts.

&nbsp;
Example 1:

Input: n = 7, cuts = [1,3,4,5]
Output: 16
Explanation: Using cuts order = [1, 3, 4, 5] as in the input leads to the following scenario:

The first cut is done to a rod of length 7 so the cost is 7. The second cut is done to a rod of length 6 (i.e. the second part of the first cut), the third is done to a rod of length 4 and the last cut is to a rod of length 3. The total cost is 7 + 6 + 4 + 3 = 20.
Rearranging the cuts to be [3, 5, 1, 4] for example will lead to a scenario with total cost = 16 (as shown in the example photo 7 + 4 + 3 + 2 = 16).

Example 2:

Input: n = 9, cuts = [5,6,1,4,2]
Output: 22
Explanation: If you try the given cuts ordering the cost will be 25.
There are much ordering with total cost &lt;= 25, for example, the order [4, 6, 5, 2, 1] has total cost = 22 which is the minimum possible.


&nbsp;
Constraints:


	2 &lt;= n &lt;= 106
	1 &lt;= cuts.length &lt;= min(n - 1, 100)
	1 &lt;= cuts[i] &lt;= n - 1
	All the integers in cuts array are distinct.


var minCost = function(n, cuts) {
    cuts = cuts.sort((a, b) => a - b);
    let map = new Map();
    // Use cutIdx to track the idx of the cut position in the cuts array
    function dfs(start, end, cutIdx) {
        let key = `${start}-${end}`;
        if (map.has(key)) return map.get(key);
        let min = Infinity
        for (let i = cutIdx; i < cuts.length; i++) {
            let cut = cuts[i];
            if (cut <= start) continue;
            if (cut < end) {
                let len = end - start;
                let left = dfs(start, cut, 0);
                let right = dfs(cut, end, i + 1);
                min = Math.min(min, len + left + right);
            } else break;
        }
        if (min === Infinity) {
            map.set(key, 0);
            return 0;
        }
        else {
            map.set(key, min);
            return min;
        }
    }
    return dfs(0, n, 0);
};
class Solution {
public:
    long cutMin(int i, int j, vector<int>&c, vector<vector<int>>&dp)
    {
        if(i>j) return 0;
        if(dp[i][j]!=-1) return dp[i][j];
        long mini = INT_MAX;
        for(int ind=i;ind<=j;ind++)
        {
            long cost = c[j+1]-c[i-1]+cutMin(i,ind-1,c,dp)+cutMin(ind+1,j,c,dp);
            mini = min(mini,cost);
        }
        return dp[i][j] = mini;
    }
    int minCost(int n, vector<int>& cuts) {
        int c = cuts.size();
        cuts.push_back(0);
        cuts.push_back(n);
        sort(cuts.begin(),cuts.end());
        vector<vector<int>>dp(c+1,vector<int>(c+1,-1));
        return cutMin(1,c,cuts,dp);
    }
};