Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, v
	}
	if len(matrices) == 0 {
		panic("must not be empty")
	} else if len(matrices) == 1 {
		return matrices[0], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < len(matrices)-1; i++ {
		formerMatrices := matrices[:i+1]
		latterMatrices := matrices[i+1:]
		matF, countF := calcMinMatrixChainMultiplication(formerMatrices, Range{0, i}, memo)
		matL, countL := calcMinMatrixChainMultiplication(latterMatrices, Range{i + 1, len(matrices) - 1}, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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])

Python: #! /usr/bin/env python3
# -*- coding: utf-8 -*-

from sys import maxsize


def irange(start, stop=None):
    if stop:
        return range(start, stop + 1)
    else:
        return range(start + 1)


def count(ms):
    n = len(ms)

    p = [0] * (n + 1)
    for i in range(n):
        p[i] = ms[i][0]
        p[i + 1] = ms[i][1]

    m = [[None] * (n + 1) for _ in irange(n)]
    for i in irange(1, n):
        m[i][i] = 0
    for l in irange(2, n):
        for i in irange(1, n - l + 1):
            j = i + l - 1
            m[i][j] = maxsize
            for k in irange(i, j - 1):
                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]


def main():
    n = int(input())

    m = []
    for _ in range(n):
        m.append(tuple(int(x) for x in input().split()))
    c = count(m)
    print(c)


if __name__ == '__main__':
    main()

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

DP = [[0]*N for _ in range(N)]
for l in range(2, N+1): # l は行列の数
    for i in range(0, N):
        j = i + l-1
        if j >= N:
            break
        
        DP_ji = 10**9
        for k in range(i, j):
            DP_ji = min(DP_ji
                        ,DP[k][i] + DP[j][k+1] + r[i]*c[k]*c[j])
        DP[j][i] = DP_ji
print(DP[N-1][0])

Python: n = int(input())
p = [0]*(n+1)
for i in range(1,n+1): p[i-1],p[i] = map(int,input().split())
m = [[0]*(n+1) for _ in range(n+1)]
# 対象とする行列の数l
for l in range(2,n+1):
    # for i in range(n+1): print(m[i])    
    # print("/////////////////////////////////")
    # 対象とする行列の範囲i,j
    # iは1からn-(l-1)まで
    for i in range(1,n-l+2):
        j = i+l-1
        m[i][j] = float("inf")
        # (M_iM_i+1...M_k)(M_k+1...M_j)の乗算回数が最小となるkを探す
        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])

Python: N = int(input())

for i in range(N):
    if i == 0:
        P = list(map(int, input().split()))  # Pは各行列の次元
        continue
    P.append(int(input().split()[1]))

# dpテーブルの用意 minを取るときはinf埋めしておくと処理が楽になるしバグが見つかりやすい
#dp[i][j]は(Mi...Mj)のminコストを入れる
INF = 2**32-1
dp = [[INF]*N for _ in range(N)]

#dpテーブルの対角成分を0で初期化
#dp[s][s]はMsのminコストを表す（Msは単独の行列）
for ij in range(N):
    dp[ij][ij] = 0

# 最終的に求めたいのはdp[0][N]だが、計算途中で繰り返すそれぞれの行列積をdpに入れておくことで効率化
for l in range(1, N):  # l は対象とする行列の個数を表す
    for i in range(N):
        j = i+l   # Mj はMiから何個の行列を考えるかで決まる
        if j >= N:  # 計算するのはインデックスはN-1まで
            continue
        for k in range(i, j):  # (Mi...Mk)(Mk+1...Mj) のようにどこでMkで区切るかを動かす
            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: import sys

p=[0 for i in range (101)]
m=[[0 for i in range(100)]for i in range(100)]

def MatrixChain(n) : 
	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('Infinity')
			for k in range(i,j) :
				q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
				m[i][j] = m[i][j] if m[i][j] <= q else q

n = int(input())
for i in range(n) :
	input_num = input()
	input_num = input_num.split(' ')
	p[i] = int(input_num[0][0])
	p[i+1] = int(input_num[1])

MatrixChain(n)
print(m[1][n])