Go: package main

/*
連鎖行列積
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
	"strings"
)

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

type mem *[][]int

func main() {
	scanner := makeScanner(1024)
	n := eGetInt(scanner)
	p := make([]int, n+1)
	for i := 0; i < n; i++ {
		rowcol := eGetInts(scanner)
		p[i] = rowcol[0]
		p[i+1] = rowcol[1]
	}
	m := make([][]int, n)
	for i := 0; i < n; i++ {
		m[i] = make([]int, n)
	}
	// 初期値の格納
	for i := 0; i < n; i++ {
		for j := i; j < n; j++ {
			if i == j {
				m[i][j] = 0 //  同じ行列へのコストは当然0
			} else {
				m[i][j] = math.MaxInt64
			}
		}
	}

	// num : 行列の数. num == 1の場合は初期値0を格納ずみ
	for num := 2; num <= n; num++ {
		// s : スタート行列番号
		for s := 0; s+num <= n; s++ {
			// t : 終了行列番号
			t := s + num - 1
			// k : 仕切
			for k := s; k < t; k++ {
				m[s][t] = min(m[s][t], m[s][k]+m[k+1][t]+p[s]*p[k+1]*p[t+1])
			}
		}
	}
	fmt.Println(m[0][n-1])
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}

// func min(a []int) int {
// 	m := a[0]
// 	for _, v := range a {
// 		if v < m {
// 			m = v
// 		}
// 	}
// 	return m
// }

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

Python: import sys
from functools import lru_cache
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


def inpl():
    return list(map(int, input().split()))


def main():
    @lru_cache(maxsize=None)
    def recur(i, j):
        # print(i, j)
        if i == j:
            return 0
        for k in range(i, j):
            c = recur(i, k) + recur(k + 1, j) + P[i] * P[k + 1] * P[j + 1]
            if c < cost[i][j]:
                cost[i][j] = c
        return cost[i][j]

    N = int(input())
    RC = [inpl() for _ in range(N)]
    cost = [[float('inf')] * (N + 1) for a in range(N + 1)]
    P = [0] * (N + 1)
    for i, rc in enumerate(RC):
        P[i] = rc[0]
    P[N] = RC[N - 1][1]

    print(recur(0, N - 1))


if __name__ == '__main__':
    main()

Python: import sys
import itertools

n = int(sys.stdin.readline())
MM = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]

amin=1000000000000
for t in itertools.permutations(range(n-1)):
    mm=[m[:] for m in MM]
    o = list(t)
    ans=0
    for _ in range(n-1):
        i = o.pop(0)
        ans += mm[i][0]*mm[i+1][1]*mm[i][1]

        mm[i][1] = mm[i+1][1]
        mm.pop(i+1)
        
        for j in range(len(o)):
            if o[j]>i: o[j] -=1

    if amin>ans: amin=ans


print(amin)

Python: N = int(input())
P = []
for i in range(N):
	ta,tb = map(int ,input().split())
	if i == N-1:
		P.append(ta)
		P.append(tb)
	else:
		P.append(ta)
inf = float('inf')
dp = [[inf] * N for _ in range(N)]
for i in range(N):
	if i == N-1:
		dp[i][i] = 0
	else:
		dp[i][i] = 0
		dp[i][i+1] = P[i]*P[i+1]*P[i+2]

for right in range(2,N):
	for i in range(N):
		j = i + right
		if j > N-1:
			break

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

		

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

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: import sys

input = sys.stdin.readline
INF = float("inf")


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

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

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

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


if __name__ == "__main__":
    main()

Python: import sys
def s():
 n=int(input())+1
 e=list(map(lambda x:x.split(),sys.stdin))
 p=[int(e[0][0])]+list(int(x[1])for x in e)
 m=[[0]*n for _ in[0]*n]
 for i in range(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: N = int(input())
R = [0]*N
C = [0]*N
for i in range(N):
    R[i],C[i] = map(int, input().split())
    
DP = [[0 for _ in range(N)] for _ in range(N)]

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

for l in range(1, N):
    for r in range(N-l):
        ri = R[r]
        le = C[r+l]
        DP[r][r+l] = min(ri*C[k]*le+DP[r][k]+DP[k+1][r+l] for k in range(r, r+l))
print(DP[0][N-1])

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