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
input = sys.stdin.readline

def matrixChainMultiplication(n):
    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            min_ = 2**21 # 十分大きい数
            for k in range(i, j):
                value = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
                if min_ > value:
                    min_ = value
            m[i][j] = min_

m = [[0]*(101) for _ in range(101)]
p = [0] * 101
N = int(input())
l0 = list(map(int, input().split()))
p[0] = l0[0]
p[1] = l0[1]
for _ in range(1, N):
    li = list(map(int, input().split()))
    p[_+1] = li[1]
matrixChainMultiplication(N)
print(m[1][N])

Python: def chain(p,n):
    m={}
    for i in range(1,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")
            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])
    print(m[1][n])

p={}
n = int(input())
for i in range(1,n+1):
    st = input()
    w = st.split()
    p[i-1] = int(w[0])
    p[i]   = int(w[1])
chain(p,n)

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: #!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)




n=int(input())
M=[0]*(n+1)
for i in range(n):
    M[i],M[i+1]=map(int,input().split())
dp=[[float('inf')]*n for i in range(n)]
for i in range(n):
    dp[i][i]=0
for k in range(1,n):
    for i in range(n-k):
        j=i+k
        for l in range(i,j):
            dp[i][j]=min(dp[i][j],dp[i][l]+dp[l+1][j]+M[i]*M[l+1]*M[j+1])
print(dp[0][n-1])
# print(dp)

Python: def matrix_chain_multiplication(n, list):
    matrix = [[0 for i in range(n)] for j in range(n)]

    for i in range(n-1):
        for j in range(n-i-1):
            k = j+i+1
            matrix[j][k] = float('inf')
            for r in range(j, k):
                matrix[j][k] = min(matrix[j][k],
                    matrix[j][r] + matrix[r+1][k]
                    + list[j]*list[r+1]*list[k+1])

    return matrix[0][n-1]

if __name__ == '__main__':
    n = int(input())

    list = list(map(int, input().split()))
    for i in range(n-1):
        list.append(int(input().split()[1]))

    print(matrix_chain_multiplication(n, list))

Python: import sys
input = sys.stdin.readline
INF = 1001001001001001
def main():
    n = int(input())
    p = [0] * (n+1)
    for i in range(n):
        p[i], p[i+1] = map(int, input().split())
    dp = [[INF]*(n+1) for _ in range(n+1)]
    for i in range(n+1):
        dp[i][i] = 0
    for l in range(2, n+1):
        for i in range(n-l+2):
            j = i + l - 1
            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])
if __name__ == '__main__': main()

Python: n = int(input())
p = list(range(n+2))

for i in range(1,n+1):
    p[i], p[n+1] = map(int, input().split())

# table[i][j] : M_i??????M_j??????????????????????°?????????°
table = [[0] * (n+1) for i in range(n+1)]

# ??£????????£???2????????????????????????
for i in range(1,n):  # 1<=i,i+1<=n
    table[i][i+1] = p[i]*p[i+1]*p[i+2]


for k in range(3, n+1):    # ?????????k???????????????????????????3<=k<=n
    for i in range(1,n+2-k): # M_i?????????1<=i<=n-k+1
        j = i + k - 1      # M_j?????§???k??????

        table[i][j] = float('inf')

        for x in range(i, j): # M_i~M_x??¨M_(x+1)~M_j??§????????????(i<=x,x+1<=j<=n)
            print(i, x, j)
            cost = table[i][x] + table[x+1][j] + p[i]*p[x+1]*p[j+1]
            table[i][j] = min(table[i][j], cost)
            

print(table[1][n])