Go: package main

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

const N = 100

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

func main() {
	var n int
	fmt.Scan(&n)
	p := make([]int, N+1)
	m := make([][]int, N+1)
	for i := range m {
		m[i] = make([]int, N+1)
	}
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 1; i <= n; i++ {
		scanner.Scan()
		p[i-1], _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		p[i], _ = strconv.Atoi(scanner.Text())
	}
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i+l-1
			m[i][j] = 1 << 21
			for k := i; k <= j-1; k++ {
				m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}
	fmt.Println(m[1][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: #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)

Python: from functools import lru_cache, reduce
from sys import stdin
from operator import mul

@lru_cache(maxsize=None)
def getMin(args:tuple):
    if len(args) == 2:
        return 0
    elif len(args) == 3:
        return reduce(mul, args)
    l = [(args[0:i+1], args[i:], args[0]*args[i]*args[-1]) for i in range(1, len(args)-1)]
    return min([getMin(v[0]) + getMin(v[1]) + v[2] for v in l])


n = int(input())
M = [tuple(map(int, line.split())) for line in stdin.readlines()]
R = tuple([M[0][0]] + [r[1] for r in M])
print(getMin(R))

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: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_B&lang=jp
#  Matrix Chain Multiplication : python3
#  2018.12.03 yonezawa

from collections import deque
import sys
input = sys.stdin.readline

def main():
    nmax = 1000000
    n = int(input())
    p = []
    calc = [[nmax for i in range(n + 1)] for i in range(n + 1)]

    for i in range(n):
        (a,b) = map(int,input().split())
        p.append(a)
    else:
        p.append(b)

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

    for i in range(1,n):
        calc[i][i+1] = p[i-1] * p[i] * p[i +1] 
    
    for s in range(2,n):
        for i in range(1,n + 1 - s ):
#            print (i,i+s)
            for j in range(s):
                m = calc[i][i+j] + calc[i+j+1][i+s] + p[i-1]*p[i+j]* p[i+s]
#                print ("m=",m,"calc:",i,i+j,"+calc",i+j+1,i+s,"p:",i-1,i+j,i+s)
                if calc[i][i+s] > m:
                    calc[i][i+s] = m
    print (calc[1][n])
if __name__ == '__main__':
    main()

Python: n = int(input())
r = [0] * (n+1) # M1=r0*r1, M2=r1*r2, ... , Mn=rn-1*rn
m = [[100**4] * (n+1) for _ in range(n+1)] # m[i][j] = Mi...Mj の最小コスト
for i in range(n):
    r[i], r[i+1] = list(map(int, input().split()))

for i in range(1, n+1): # 1個の行列のコスト
    m[i][i] = 0
for i in range(1, n): # 2個の行列のコスト
    m[i][i+1] = r[i-1] * r[i] * r[i+1]
for j in range(2, n): # j+1 個の行列のコスト
    for i in range(1, n-j+1): # Mi...Mi+j
        for k in range(j): # Mi...Mi+k と Mi+k+1...Mi+j に分割
            # print('i=',i,' i+k=',i+k,' i+j=', i+j)
            cost = m[i][i+k] + r[i-1]*r[i+k]*r[i+j] + m[i+k+1][i+j]
            if cost < m[i][i+j]:
                m[i][i+j] = cost
print(m[1][n])