You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex (i.e., clockwise order).

You will triangulate the polygon into n - 2 triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all n - 2 triangles in the triangulation.

Return the smallest possible total score that you can achieve with some triangulation of the polygon.

&nbsp;
Example 1:

Input: values = [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.


Example 2:

Input: values = [3,7,4,5]
Output: 144
Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
The minimum score is 144.


Example 3:

Input: values = [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.


&nbsp;
Constraints:


	n == values.length
	3 &lt;= n &lt;= 50
	1 &lt;= values[i] &lt;= 100


class Solution:
    def minScoreTriangulation(self, values: List[int]) -> int:
        
        n = len(values)
        
        c = [[0 for _ in range(n)] for _ in range(n)]
        
        for L in range(2, n):
            
            for i in range(1, n-L+1):
                
                j = i + L - 1
                
                c[i][j] = float('inf')
                
                for k in range(i, j):
                    
                    q = c[i][k] + c[k+1][j] + (values[i-1]*values[k]*values[j])
                    
                    if c[i][j] > q:
                        c[i][j] = q
        
        return c[1][n-1]
                
                
        
            
        
class Solution {
    int solve(int[] v, int i, int j){
        if(i+1==j)
            return 0;
        int ans= Integer.MAX_VALUE;
        for(int k=i+1;k<j;k++){
            ans= Math.min(ans, (v[i]*v[j]*v[k] + solve(v,i,k) + solve(v,k,j) ) );
        }
        return ans;
    }

    int solveMem(int[] v, int i, int j, int[][] dp){
      if(i+1==j)
            return 0;

        if(dp[i][j]!=-1)
            return dp[i][j];

       int ans= Integer.MAX_VALUE;

        for(int k=i+1;k<j;k++){
          ans = Math.min(ans, (v[i]*v[j]*v[k] + solveMem(v,i,k,dp) + solveMem(v,k,j,dp) ) );
        }
        dp[i][j]=ans;
        return dp[i][j];
    }

    int solveTab(int[] v){
        int n= v.length;
        int[][] dp= new int[n][n];

        for(int i=n-1;i>=0;i--){
            for(int j=i+2;j<n;j++){

                int ans=Integer.MAX_VALUE;
                 for(int k=i+1;k<j;k++){
                    ans = Math.min(ans, (v[i]*v[j]*v[k] + dp[i][k] + dp[k][j] ) );
                    }
                    dp[i][j]=ans;
            }
        }

        return dp[0][n-1];

        }

    public int minScoreTriangulation(int[] values) {
        int n= values.length;
       // return solve(values, 0, n-1); // For Recursion

    /* int[][] dp= new int[n][n]; // For Top-Down DP(Memoisation)
        for(int[] row:dp){
            Arrays.fill(row,-1);
        }

        return solveMem(values,0,n-1,dp);
        */

        return solveTab(values); //For Bottom-Down DP(Tabulisation)
    }
}
class Solution {
public:
	int f(int i,int j,vector<int>& values,vector<vector<int>> &dp){
		if(i == j || i+1 == j)    return 0;
		if(dp[i][j] != -1)        return dp[i][j];
		int res = INT_MAX;
		for(int k = i+1;k < j;k++)
		{
			int temp = values[i]*values[j]*values[k] + f(k,j,values,dp) + f(i,k,values,dp);
			res = min(res,temp);
		}
		return dp[i][j] = res;
	}

	int minScoreTriangulation(vector<int>& values) {
		int n = values.size();
		vector<vector<int>> dp(n+1,vector<int>(n+1,-1));
		return f(0,n - 1,values,dp);
	}
var minScoreTriangulation = function(values) {
    let dp = Array(values.length).fill().map((i) => Array(values.length).fill(0));
    function dfs(i, j) {
        if (dp[i][j]) return dp[i][j];
        if (j - i < 2) return 0;
        let min = Infinity;
        // k forms a triangle with i and j, thus bisecting the array into two parts
        // These two parts become two subproblems that can be solved recursively
        for (let k = i + 1; k < j; k++) {
            let sum = values[i] * values[j] * values[k] + dfs(i, k) + dfs(k, j);
            min = Math.min(min, sum);
        }
        return dp[i][j] = min;
    }
    return dfs(0, values.length - 1);
};
Minimum Score Triangulation of Polygon