class Solution:
def minScoreTriangulation(self, A):
length = len(A)
inf = float('inf')
dp = [[inf for _ in range(length)] for _ in range(length)]
for i in range(length - 1):
dp[i][i + 1] = 0
for d in range(2, length):
for i in range(0, length - d):
j = i + d
for k in range(i + 1, j):
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + A[i] * A[k] * A[j])
return dp[0][length - 1]