Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: import sys

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

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

for i in range(n):
    p[i], p[i+1] = map(int, sys.stdin.readline().strip().split())

# dpテーブルの対角成分を初期値とし、右斜に更新していく
# 利用する行列の数
for l in range(2, n+1):
    # 利用する範囲（l個の行列のうちi - jの範囲の行列を扱う）
    for i in range(1, n-l+2):
        j = i + l - 1
        dp[i][j] = float("inf")
        # 掛け合わせの区切り位置
        for k in range(i, j):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1]*p[k]*p[j])

print(dp[1][n])

Python: n = int(input())
size = [int(i) for i in input().split()]
for _ in range(n - 1):
    _, a = map(int, input().split())
    size.append(a)
dp = [[0] * (n) for _ in range(n)]
# init
for i in range(n):
    dp[i][i] = 0
# solve
for l in range(1, n):
    for i in range(n - l):
        j = i + l
        min = 10 ** 9
        for k in range(i, j):
            tmp = dp[i][k] + dp[k + 1][j] + size[i] * size[k + 1] * size[j + 1]
            if tmp < min:
                min = tmp
        dp[i][j] = min
print(dp[0][-1])

Python: def s():
 n=int(input())+1
 e=[input().split()for _  in[0]*~-n]
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(1,n):
  for r in range(n-i-1):
   c=r+i+1
   for j in range(r+1,c):
    x=m[r][j]+m[j][c]+p[r]*p[j]*p[c]
    if 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print(m[r][c])
if'__main__'==__name__:s()

Python: MAX_R = 100
MAX_C = 100
n = int(input())
mat_list = [list(map(int, input().split())) for _ in range(n)]
dp = [[-1] * (MAX_C + 1) for _ in range(MAX_R + 1)]

def get_min(i, j):
    ans = float('inf')
    if dp[i][j] != -1:
        return dp[i][j]
    if i == j:
        ans = 0
    else:
        for k in range(i, j):
            p = mat_list[i][0]
            q = mat_list[k][1]
            r = mat_list[j][1]
            tmp = get_min(i, k) + get_min(k+1, j) + p*q*r
            if tmp < ans:
                ans = tmp
    dp[i][j] = ans
    return ans

print(get_min(0, n-1))

Python: import sys
sys.setrecursionlimit(10**7)
debug = False
import numpy as np
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 = np.zeros(shape=(n, 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: #coding:utf-8
import numpy as np

n = int(input())
m = [[0 for i in range(n)] for j in range(n)]
m = np.array(m)
p = []
for i in range(n):
    p1,p2 = map(int,input().split())
    if i == 0:
        p.append(p1)
    p.append(p2)

def matrixChainMultiplication():
    for i in range(n):
        m[i,i] = 0

    for l in range(2,n+1):
        for i in range(0,n-l+1):
            j = i + l - 1
            m[i,j] = 1000000
            for k in range(i,j):
                m[i,j] = min(m[i,j], m[i,k] + m[k+1,j] + p[i] * p[k+1] * p[j+1])
matrixChainMultiplication()
print(m[0,n-1])

Python: n = int(input())

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

for i in range(n):
    r[i], c[i] = map(int, input().split())

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

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

for gap in range(1, n):
    for s in range(n):
        e = s + gap
        if e >= n:
            continue
        for k in range(s, e):
            dp[s][e] = min(dp[s][e], dp[s][k] + dp[k + 1][e] + r[s] * c[k] * c[e])

print(dp[0][n - 1])

Python: def matrixChainMultiplication(M):
    # P ??????
    n = len(M)
    P = [M[0][0]]
    P += [M[i][1] for i in range(n)]
    # ?¨???¶?????? m ??????
    m = [[[] for _ in range(n + 1)] for _ in range(n + 1)]
    for i in range(n + 1):
            m[i][i] = 0

    # l???????±???¨?????????????????°
    for l in range(2, n + 1):
        for i in range(1, n - l + 2):
            j = i + l - 1
            m[i][j] = 9999999999999999
            for k in range(i, j):
                m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + P[i -1] * P[k] * P[j])
    return m[1][n]


if __name__ == '__main__':
    n = int(input())
    M = [list(map(int, input().split())) for _ in range(n)]
    print(matrixChainMultiplication(M))

Python: n = input().split()

nele = int(n[0])
list = range(1,nele+1)

nrow = {}
ncol = {}
for i in list:
	nrow[i] = int(n[i*2-1])
	ncol[i] = int(n[i*2])

#print(nrow, ncol)


min = -1
cur = -1
route = []
done = []

for i in list:
	route.append(0)

#print(route)

depth = -1
min = -1
def tansaku(prev):
	
	global depth
	depth += 1
	
	global nrow
	global ncol
	global min
	global route
	
	done = route[:]
	
	for i in list:
#		print(depth, i, route, done)
		
		if i in done:
			continue
		
		next = i
		done.append(next)
		
		if depth == 0:
			route[depth] = next
			tmp = 0
			min = tansaku(tmp)
			continue
		
		tmp = prev + nrow[route[depth-1]] * ncol[next]
#		print(tmp)
		
		if tmp > min and min != -1:
#			print("aaa")
			continue
		else:
			route[depth] = next
			if depth >= nele-1:
				print("bbb")
				min = tmp
			else:
#				print("ccc")
				min = tansaku(tmp)
	
	depth -= 1
	route[depth] = 0
#	print("min:", min)
	return min

min = tansaku(0)

print(min)