Go: package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func min(x, y int) int {
	if x > y {
		return y
	}
	return x
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)

	P := make([]int, n+1)
	for i := 1; i <= n; i++ {
		l := nextInt(sc)
		P[i-1] = l
		m := nextInt(sc)
		P[i] = m
	}

	matrix := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		matrix[i] = make([]int, n+1)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n - l + 1; i++ {
			j := i + l - 1
			matrix[i][j] = (1 << 21)
			for k := i; k <= j-1; k++ {
				matrix[i][j] = min(matrix[i][j], matrix[i][k] + matrix[k+1][j] + P[i-1] * P[k] * P[j])
			}
		}
	}
	fmt.Println(matrix[1][n])
}

Python: import sys
sys.setrecursionlimit(10**7)
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())



def solve():
    n = int(input())
    mat_list = []
    for i in range(n):
        mat_list.append(list(map(int, input().split())))
    dprint(mat_list)

    # 掛け始めのidxと掛け終わりのidxをi, jとし、計算回数を持つメモ
    memo = [[0 for i in range(n)] for i in range(n)]

    def calc(start, end):
        dprint("*", start, end)
        # 1つの行列なら0
        if end == start:
            return 0

        # メモがあれば返す
        if memo[start][end] != 0:
            return memo[start][end]

        # 2つの行列なら、答えを計算しメモ追記
        if end - start == 1:
            cost = mat_list[start][0] * mat_list[start][1] * mat_list[end][1]
            memo[start][end] = cost
            dprint(start, end, cost)
            return cost

        # 3つ以上なら、再帰
        min_cost = -1
        if end - start >= 2:
            for right_start in range(start+1, end+1):
                left = calc(start, right_start-1)
                right = calc(right_start, end)
                cost = left + right + mat_list[start][0] * mat_list[right_start][0] * mat_list[end][1]
                if min_cost == -1 or min_cost > cost:
                    min_cost = cost
                dprint(start, end, right_start, min_cost, cost)

        memo[start][end] = min_cost
        return min_cost

    ans = calc(0, n-1)
    dprint(memo)
    print(int(ans))

solve()

Python: N = int(input())

P = []
for i in range(N):
    if i == 0:
        P = list(map(int, input().split()))
        continue
    P.append(int(input().split()[1]))

dp = [[float("inf")]*N for _ in range(N)]

for i in range(N):
    dp[i][i] = 0

for l in range(1, N):  
    for i in range(N):
        j = i+l
        if j < N:
            for k in range(i, j): 
                dp[i][j] = min(dp[i][k] + dp[k+1][j] + P[i]*P[j+1]*P[k+1], dp[i][j])

print(dp[0][-1])

Python: N = int(input())
R = [0]*N
C = [0]*N
for i in range(N):
    R[i], C[i] = map(int, input().split())

INF = 10**18
dp = [[INF]*N for i in range(N)]
for i in range(N):
    dp[i][i] = 0
for l in range(1, N):
    for i in range(N-l):
        a0 = R[i]
        c0 = C[i+l]
        dp[i][i+l] = min(a0*C[j]*c0 + dp[i][j] + dp[j+1][i+l] for j in range(i, i+l))
print(dp[0][N-1])

Python: import sys

n = int(sys.stdin.readline())

m = list(map(int, sys.stdin.readline().split()))
for _ in range(n-1):
    _, a = sys.stdin.readline().split()
    m.append(int(a))

c = [[-1 for _ in range(n)] for _ in range(n)]

def val(a, b):
    if a==b: return 0
    if c[a][b]>0: return c[a][b]
    c[a][b]=val(a+1,b)+m[a-1]*m[a]*m[b]
    for k in range(a+1, b):
        v=val(a,k)+val(k+1,b)+m[a-1]*m[k]*m[b]
        if c[a][b]>v: c[a][b]=v
    return c[a][b]

print(val(0, n-1))

Python: def least_multiplication(mtx):
    N = len(mtx)
    mul = [[None] * N for x in range(N)]

    for i in range(N):
        mul[i][i] = 0
    #pprint.pprint(mul)

    for chain in range(1, N):
        for begin in range(N-chain):
            end = begin + chain
            cand = []
            for j in range(begin, end):
                left = mul[begin][j]
                down = mul[j+1][end]
                this = mtx[begin][0] * mtx[j][1] * mtx[end][1]
                cand.append(left + down + this)
            mul[begin][begin+chain] = min(cand)
        #pprint.pprint(mul)
    return mul[0][N-1]


def main():
    n = int(input())
    matrices = []
    for i in range(n):
        col, row = map(int, input().split())
        matrices .append((col, row))
    ans = least_multiplication(matrices)
    print(ans)


main()

Python: import sys

sys.setrecursionlimit(10**7)

inf = float('inf')

def solve():
    n = int(sys.stdin.readline())

    r = [0] * n
    c = [0] * n

    for i in range(n):
        ri, ci = map(int, sys.stdin.readline().split())
        r[i] = ri
        c[i] = ci

    dp = [[0]*(n + 1) for i in range(n + 1)]

    for ls in range(2, n + 1):
        for i in range(n - ls + 1):
            dp[i][i + ls] = inf
            for j in range(i + 1, i + ls):
                dp[i][i + ls] = min(dp[i][i + ls], dp[i][j] + dp[j][i + ls] + r[i]*r[j]*c[i + ls - 1])

    ans = dp[0][n]

    print(ans)

if __name__ == '__main__':
    solve()

Python: def solve(n,mat):
    dp=[[0]*n for _ in range(n)]
    for kukan in range(1,n):
        for i in range(n-kukan):
            dp[i][i+kukan]=min([dp[i][mid]+dp[mid+1][i+kukan]+mat[i][0]*mat[mid+1][0]*mat[i+kukan][1] for mid in range(i,i+kukan)])
    return dp[0][n-1]

n=int(input())
mat=[tuple(map(int,input().split())) for _ in range(n)]
print(solve(n,mat))

Python: # -*- coding: utf-8 -*-

n = int(input())
p = []
m = dict()
for _ in range(n):
    r, c = map(int, input().split())
    p.append(r)
    if _ == n-1:
        p.append(c)

for i in range(n+1):
    m[(i,i)] = 0

for l in range(2, n + 1):
    for i in range(1, n - l + 2):
        j = i + l - 1
        m[(i, j)] = float('inf')
        #print(l,i,j)
        for k in range(i, j):
            value = m[(i, k)] + m[(k+1, j)] + p[i-1]*p[k]*p[j]
            m[(i, j)] = min(m[(i, j)], value)

print(m[(1, n)])

Python: N = int(input())
RC = [tuple(map(int, input().split())) for _ in range(N)]
INF = 10**18
# dp[i][j]: iからjの区間における最小回数
dp = [[INF]*N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            dp[i][j] = 0
for diff in range(2, N+1):
    for i in range(N-diff+1):
        j = i+diff-1
        for k in range(i, j):
            dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+RC[i][0]*RC[k][1]*RC[j][1])
print(dp[0][N-1])

Python: import sys
input = sys.stdin.readline
a = []
dp = []

#l <= x < rまでの行列せきの最小値
def rec(l, r):
	if dp[l][r] != -1:
		return dp[l][r]
	if l+1 == r:
		dp[l][r] = 0
		return dp[l][r]
	if l+2 == r:
		dp[l][r] = a[l][0] * a[l][1] * a[l+1][1]
		return dp[l][r]

	m = 100000000
	for i in range(l+1, r):
		m = min(m, rec(l, i) + rec(i, r) + a[l][0] * a[i][0] * a[r-1][1])
	dp[l][r] = m
	return dp[l][r]

def main():
	global a, dp
	n = int(input().strip())
	for i in range(n):
		a.append(tuple(map(int, input().strip().split())))
	dp = [[-1] * (n+1) for _ in range(n)]
	print(rec(0, n))

if __name__ == '__main__':
	main()

Python: import sys


class Mat(object):

  def __init__(self, row, col):
    self.row = row
    self.col = col
    
def get_min_mult_cnt_dp(i, j, mat_list):

  multp = [[sys.maxsize for i in range(len(mat_list))] for i in range(len(mat_list))]

  

  # 長さ0連鎖の演算量
  for i in range(len(mat_list)):
    multp[i][i] = 0

  for le in range(1, len(mat_list)):
    for i in range(len(mat_list)-le):
      j = i + le

      tmp = sys.maxsize
      for k in range(i, j):
        p = mat_list[i].row
        n = mat_list[k].col
        q = mat_list[j].col
        tmp = multp[i][k] + multp[k+1][j] + p*n*q

        if (tmp < multp[i][j]):
          multp[i][j] = tmp
    
  
  return multp[i][j]
mat_list = []
num = int(input())
for i in range(num):
    a,b = map(int,input().split())
    mat_list.append(Mat(a,b))



print(int(get_min_mult_cnt_dp(0, num-1, mat_list)))