Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	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
			//kまでで区切る
			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])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

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

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[1][n])
}

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

Python: import itertools
n = int(input())
rc = [list(map(int, input().split())) for _ in range(n)]
rc_min = rc.index(min(rc))

def calc(rc):
    total = 0
    for i, irc in enumerate(rc):
        if i ==0:
            r, c = irc
        else:
            total += r * c * irc[1]
            c = irc[1]
    return r, c, total
def reverse_calc(rc):
    total = 0
    for i, irc in enumerate(rc[::-1]):
        if i ==0:
            r, c = irc
        else:
            total += r * c * irc[0]
            r = irc[0]
    return r, c, total

total = 0
r1, c1, ret = reverse_calc(rc[:rc_min])
total += ret
r2, c2, ret =calc(rc[rc_min:])
total += ret
total += r1 * c1 * c2
print(total)

Python: def main():
    """連鎖行列積を求める.    
    """
    n = int(input())
    inf = 10**10
    if n == 0 or n == 1:
        print(0)
        return
    if n > 1:
        p = [int(input().split()[0]) for _ in range(n-1)]
    p.extend(list(map(int, input().split())))
    m = [[inf*(i!=j) for i in range(n)] for j in range(n)]
    for l in range(1, n):
        for i in range(n-l):
            j = i+l
            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])
    print(m[0][-1])

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+1): # 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])

Python: n = int(input())
INF = 10**9
P = [list(map(int, input().split())) for i in range(n)]
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + P[i][0] * P[k][1] * P[j][1])

print(m[0][n - 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: import sys
 
n = int(input())
 
mlist = [0]*(n+1)
#print(mlist)
mlist[0],mlist[1] = map(int,input().split())
#print(mlist)
for i in range(2,n+1):
    mlist[i] = int(input().split()[1])
    #print(mlist)
dp = [[sys.maxsize]*(n+1) for i in range(n+1)]
 
for i in range(1,n+1):
    dp[i][i] = 0
 
for l in range(2,n+1) :
    for i in range(1,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]+mlist[i-1]*mlist[k]*mlist[j])
 
print(dp[1][n])