Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], ..., A[N-1] in clockwise order.
Suppose you triangulate the polygon into N-2 triangles.  For each triangle, the value of that triangle is the product of the labels of the 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.
 



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


Example 2:

Input: [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: [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.

 
Note:

3 <= A.length <= 50
1 <= A[i] <= 100

Difficulty: MEDIUM
Tags: Dynamic programming

Data structures
Skill types: Dynamic programming

Data structures

Starter code:
class Solution:
    def minScoreTriangulation(self, A: List[int]) -> int:
        

Solutions:
class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		N = len(A)
		dp = [[0] * N for _ in range(N)]
		for i in range(N - 2, -1, -1):
			for j in range(i + 2, N):
				dp[i][j] = min((dp[i][k] + dp[k][j] + A[i] * A[j] * A[k] for k in range(i + 1, j)))
		return dp[0][-1]


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		memo = [[0] * len(A) for _ in range(len(A))]
		for i in reversed(range(len(A))):
			for j in range(i + 2, len(A)):
				memo[i][j] = min((memo[i][k] + A[i] * A[k] * A[j] + memo[k][j] for k in range(i + 1, j)))
		return memo[0][-1]


from functools import lru_cache

class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		@lru_cache(None)
		def dp(i, j):
			return min([dp(i, k) + dp(k, j) + A[i] * A[k] * A[j] for k in range(i + 1, j)] or [0])
		return dp(0, len(A) - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		@lru_cache(maxsize=None)
		def helper(start, end):
			if start + 1 == end:
				return 0
			result = 0
			for k in range(start + 1, end):
				result = min(float('inf') if result == 0 else result, helper(start, k) + A[start] * A[k] * A[end] + helper(k, end))
			return result
		return helper(0, len(A) - 1)


class Solution:

	def minScoreTriangulation(self, a: List[int]) -> int:
		n = len(a)
		dp = [[float('inf')] * n for _ in range(n)]
		for l in range(2, n):
			for i in range(n - l):
				j = i + l
				for k in range(i + 1, j):
					dp[i][j] = min(dp[i][j], (dp[i][k] if k >= i + 2 else 0) + a[i] * a[k] * a[j] + (dp[k][j] if j >= k + 2 else 0))
		return dp[0][n - 1]


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		n = len(A)
		dp = [[0] * n for i in range(n)]
		for d in range(2, n):
			for i in range(n - d):
				j = i + d
				dp[i][j] = min((dp[i][k] + dp[k][j] + A[i] * A[j] * A[k] for k in range(i + 1, j)))
		return dp[0][n - 1]


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		@lru_cache(None)
		def dfs(i, j):
			if j < i + 2:
				return 0
			result = 10 ** 9
			for k in range(i + 1, j):
				result = min(result, A[i] * A[k] * A[j] + dfs(i, k) + dfs(k, j))
			return result
		n = len(A)
		return dfs(0, n - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		N = len(A)
		dp = [[0 for j in range(N)] for i in range(N)]
		for l in range(3, N + 1):
			for i in range(N - l + 1):
				j = i + l - 1
				for k in range(i + 1, j):
					val = dp[i][k] + dp[k][j] + A[i] * A[k] * A[j]
					if not dp[i][j] or dp[i][j] > val:
						dp[i][j] = val
		return dp[0][N - 1]


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		def dfs(nums, i, j):
			if j - i <= 1:
				return 0
			if dp[i][j] != 0:
				return dp[i][j]
			ans = sys.maxsize
			for k in range(i + 1, j):
				ans = min(ans, dfs(nums, i, k) + dfs(nums, k, j) + nums[i] * nums[k] * nums[j])
			dp[i][j] = ans
			return ans
		n = len(A)
		dp = [[0] * n for _ in range(n)]
		total = dfs(A, 0, len(A) - 1)
		return total


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		n = len(A)
		dp = [[0 for i in range(n)] for j in range(n)]
		for length in range(2, n):
			for i in range(n - length):
				j = i + length
				dp[i][j] = math.inf
				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 sum(A) if dp[0][n - 1] == math.inf else dp[0][n - 1]


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		memo = {}

		def dp(i, j):
			if (i, j) not in memo:
				memo[i, j] = min([dp(i, k) + dp(k, j) + A[i] * A[j] * A[k] for k in range(i + 1, j)] or [0])
			return memo[i, j]
		return dp(0, len(A) - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		table = [[-1 for i in range(len(A))] for j in range(len(A))]

		def recursion(x=0, y=len(A) - 1, A: list=A):
			if y - x <= 1:
				return 0
			if table[x][y] != -1:
				return table[x][y]
			else:
				m = float('inf')
				for i in range(x + 1, y):
					m = min(m, recursion(x, i, A) + recursion(i, y, A) + A[x] * A[y] * A[i])
				table[x][y] = m
				return m
		return recursion()


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		def get(l, r):
			if r - l == 1:
				return 0
			if (l, r) in d:
				return d[l, r]
			res = min([A[l] * A[r] * A[i] + get(l, i) + get(i, r) for i in range(l + 1, r)])
			d[l, r] = res
			return res
		d = {}
		return get(0, len(A) - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		if len(A) == 3:
			return A[0] * A[1] * A[2]
		n = len(A)
		dp = [[float('inf') for j in range(n)] for i in range(n)]

		def helper(i, j):
			if j - i <= 1:
				return 0
			if dp[i][j] != float('inf'):
				return dp[i][j]
			res = float('inf')
			for k in range(i + 1, j):
				res = min(res, helper(i, k) + A[i] * A[k] * A[j] + helper(k, j))
			dp[i][j] = res
			return dp[i][j]
		return helper(0, n - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		N = len(A)
		if N == 3:
			return A[0] * A[1] * A[2]

		def solver(s, t, A):
			if t - s + 1 < 3:
				return 0
			if dp[s][t] != float('inf'):
				return dp[s][t]
			for i in range(s + 1, t):
				dp[s][t] = min(dp[s][t], solver(s, i, A) + A[s] * A[i] * A[t] + solver(i, t, A))
			return dp[s][t]
		dp = []
		for i in range(N):
			dp.append([float('inf')] * N)
		return solver(0, N - 1, A)


class Solution:

	def minScoreTriangulation_v2(self, A: List[int]) -> int:
		length = len(A)
		DP = [[float('inf')] * length for _ in range(len(A))]
		for e in range(2, length):
			for i in range(length - e):
				j = i + e
				for k in range(i + 1, j):
					left_point = DP[i][k] if k >= i + 2 else 0
					right_point = DP[k][j] if j >= k + 2 else 0
					DP[i][j] = min(DP[i][j], left_point + A[i] * A[j] * A[k] + right_point)
		return DP[0][len(A) - 1]

	def minScoreTriangulation(self, A: List[int]) -> int:
		length = len(A)
		memo = [[float('inf')] * length for _ in range(len(A))]

		def memo_dp(start, end):
			if end - start < 2:
				return 0
			if end - start == 2:
				memo[start][end] = A[start] * A[start + 1] * A[end]
				return memo[start][end]
			if memo[start][end] != float('inf'):
				return memo[start][end]
			val = memo[start][end]
			for i in range(start + 1, end):
				val = min(val, A[start] * A[i] * A[end] + memo_dp(start, i) + memo_dp(i, end))
			memo[start][end] = val
			return memo[start][end]
		memo_dp(0, length - 1)
		return memo[0][length - 1]


def tri_score(a1, a2, a3):
	return a1 * a2 * a3

class Solution:

	def __init__(self):
		self.memory = []

	def minScoreTriangulation(self, A) -> int:
		n = len(A)
		self.memory = [[None for _ in range(n)] for _ in range(n)]
		return self.minScoreTriangulationPart(A, 0, len(A) - 1)

	def minScoreTriangulationPart(self, A, start: int, end: int) -> int:
		if end - start < 2:
			return 0
		elif self.memory[start][end]:
			return self.memory[start][end]
		elif end - start == 2:
			res = tri_score(A[start], A[start + 1], A[start + 2])
			self.memory[start][end] = res
			return res
		res = min((tri_score(A[start], A[end], A[k]) + self.minScoreTriangulationPart(A, start, k) + self.minScoreTriangulationPart(A, k, end) for k in range(start + 1, end)))
		self.memory[start][end] = res
		return res


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		dp = {}

		def recursive_helper(left, right):
			if left > right:
				return 0
			pair = (left, right)
			if pair in dp:
				return dp[pair]
			dp[pair] = float('inf')
			for k in range(left + 1, right):
				dp[pair] = min(dp[pair], recursive_helper(left, k) + recursive_helper(k, right) + A[left] * A[k] * A[right])
			if dp[pair] == float('inf'):
				dp[pair] = 0
			return dp[pair]
		return recursive_helper(0, len(A) - 1)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		dp = dict()

		def helper(i, j):
			if (i, j) in dp:
				return dp[i, j]
			if j - i <= 1:
				dp[i, j] = 0
				return dp[i, j]
			minval = float('inf')
			for k in range(i + 1, j):
				val = helper(i, k) + helper(k, j) + A[i] * A[j] * A[k]
				minval = min(minval, val)
			dp[i, j] = minval
			return dp[i, j]
		return helper(0, len(A) - 1)

	def minScoreTriangulation3(self, A: List[int]) -> int:
		if len(A) < 3:
			return 0
		if len(A) == 3:
			return A[0] * A[1] * A[2]

		def multi_array(B, j):
			return B[j % len(B)] * B[(j + 1) % len(B)] * B[(j + 2) % len(B)]
		(sum1, sum2) = (0, 0)
		A_copy = A.copy()
		sum1 += multi_array(A, 0)
		A_copy.pop(1)
		sum1 += self.minScoreTriangulation(A_copy)
		sum1 += multi_array(A, len(A) - 1)
		A.pop(0)
		sum2 += self.minScoreTriangulation(A_copy)
		return min(sum1, sum2)

	def minScoreTriangulation2(self, A: List[int]) -> int:
		if len(A) < 3:
			return 0
		if len(A) == 3:
			return A[0] * A[1] * A[2]

		def multi_array(B, j):
			print(B[j % len(B)], B[(j + 1) % len(B)], B[(j + 2) % len(B)])
			return B[j % len(B)] * B[(j + 1) % len(B)] * B[(j + 2) % len(B)]
		(sum1, sum2) = (0, 0)
		next_A = []
		for i in range(len(A) // 2):
			sum1 += multi_array(A, i * 2)
			print(i, sum1, multi_array(A, i * 2))
			next_A.append(A[i * 2 % len(A)])
		if (i * 2 + 2) % len(A) != 0:
			next_A.append(A[(i * 2 + 2) % len(A)])
		print(next_A, sum1)
		sum1 += self.minScoreTriangulation(next_A)
		print(sum1, '')
		val = A.pop(0)
		A.append(val)
		next_A = []
		for i in range(len(A) // 2):
			sum2 += multi_array(A, i * 2)
			print(i, sum2)
			next_A.append(A[i * 2 % len(A)])
		if (i * 2 + 2) % len(A) != 0:
			next_A.append(A[(i * 2 + 2) % len(A)])
		print(next_A, sum2)
		sum2 += self.minScoreTriangulation(next_A)
		return min(sum1, sum2)


class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:

		def helper(i, j):
			key = (i, j)
			if key in d:
				return d[key]
			if j - i < 2:
				return 0
			ans = float('inf')
			for k in range(i + 1, j):
				lans = helper(i, k)
				rans = helper(k, j)
				ans = min(ans, lans + rans + A[i] * A[j] * A[k])
			d[key] = ans
			return d[key]
		d = dict()
		return helper(0, len(A) - 1)


from functools import lru_cache

class Solution:

	def minScoreTriangulation(self, A: List[int]) -> int:
		n = len(A)
		dp = [[0] * n for _ in range(n)]
		for d in range(2, n):
			for i in range(n - d):
				j = i + d
				dp[i][j] = min((dp[i][k] + dp[k][j] + A[i] * A[j] * A[k] for k in range(i + 1, j)))
		return dp[0][n - 1]