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: def main():
    N = int(input())
    p = []
    for i in range(N):
        r, c = (int(i) for i in input().split())
        p.append(r)
        if i == N - 1:
            p.append(c)
    memo = [[float('inf') for j in range(N + 1)] for i in range(N + 1)]

    def matrix_chain_order():
        for i in range(1,N+1):
            memo[i][i] = 0

        for l in range(2,N+1):
            for i in range(1, N - l + 1 + 1):
                j = i + l - 1
                for k in range(i, j):
                    q = memo[i][k] + memo[k + 1][j] + p[i-1]*p[k]*p[j]
                    if q < memo[i][j]:
                        memo[i][j] = q

    matrix_chain_order()
    print(memo[1][N])

if __name__ == '__main__':
    main()

Python: N = int(input())
m = []
for i in range(N):
  x,y = map(int,input().split())
  m.append((x,y))

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

for L in range(1,N): 
  for i in range(N-L): 
    j = i+L
    dp[i][j] = float('inf')
    for k in range(i,j):
      m0,m1,m2 = m[i],m[k],m[j]
      # print(i,j,k,m0,m1,m2)
      dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+m0[0]*m1[1]*m2[1])

print(dp[0][-1])

Python: import sys

# sys.stdin = open('input.txt')

N = 100 + 1

p = [0] * N
m = [[0] * N for _ in range(N)]

n = int(input())

for i in range(1, n+1):
    p[i-1], p[i] = map(int, input().split())
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+1+1):
        j = i + l - 1
        m[i][j] = (1 << 21)
        for k in range(i, j-1+1):
            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: def main():
    n = int(input())
    m1 = input().split()
    ps = [int(m1[0]), int(m1[1])]
    for _ in range(n - 1):
        ps.append(int(input().split()[1]))
    ms = [[float("inf") for _ in range(n)] for __ in range(n)]
    # 範囲 0 - n
    for i in range(n):
        ms[i][i] = 0

    for length in range(1, n):
        # 00 11 22 33 44 ...         nn
        # 01 12 23 34 45 ...      n-1,n
        # 02 13 24 35 46 ... n-2,n
        # ...
        # 0n
        for start in range(0, n - length):
            end = start + length
            # 仕切りpartition
            m_start_end = float("inf")
            for partition in range(start, end):
                m_start_end = min(m_start_end, ms[start][partition] + ms[partition + 1]
                                  [end] + ps[start] * ps[partition + 1] * ps[end + 1])
            ms[start][end] = m_start_end
    print(ms[0][n - 1])


if __name__ == "__main__":
    main()

Python: 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 l in range(2,n):
 for i in range(1,n-l+1):
  j=i+l-1;m[i][j]=1e6
  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-1])

Python: import sys
INF = float("inf")
n = int(input())
m = [[0] * (n + 1) for _ in range(n + 1)]
li = [list(map(int, a.split())) for a in sys.stdin]
z = (li[0][0], ) + list(zip(*li))[1]
for l in range(1, n):
    for i in range(1, n - l + 1):
        j = i + l
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + z[i - 1] * z[k] * z[j])
print(m[1][n])

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

dp = [0 for _ in range(n)]
for i in range(n-1):
    dp[i] = data[i][0] * data[i][1] * data[i+1][1]



def mul(i, j):
    if i == j:
        return 0
    elif i == j - 1:
        return dp[i]
    num = 100000
    for k in range(i, j):
        num = min(num, mul(i, k) + mul(k+1, j) + data[i][0] * data[k][1] * data[j][1])
    return num

print(mul(0, n-1))

Python: INF = 1145141919810364364334

n = int(input())
p = []
for i in range(n):
    a,b = map(int,input().split())
    if i==0:
        p.append(a)
        p.append(b)
    else:
        p.append(b)
    
cost = [[INF] * (n+1) for i in range(n+1)]
for i in range(n+1):
    cost[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):
            cost[i][j] = min(cost[i][j] , cost[i][k] + cost[k+1][j] + p[i-1]*p[k]*p[j])
            
print(cost[1][n])

Python: import sys
from typing import List


if __name__ == "__main__":
    num_matrix = int(input())
    matrices: List[List[int]] = []
    for _ in range(num_matrix):
        matrices.append(list(map(lambda x: int(x), input().split())))
    dp = [[sys.maxsize] * num_matrix for i in range(num_matrix)]
    for i in range(num_matrix):
        dp[i][i] = 0

    for l in range(1, num_matrix):
        for i in range(num_matrix - l):
            row = matrices[i][0]
            col = matrices[i + l][1]
            dp[i][i + l] = min(row * matrices[j][1] * col + dp[i][j] + dp[j + 1][i + l]
                               for j in range(i, i + l))

    print(f"{dp[0][num_matrix - 1]}")

Python: import sys
def calc(i,j):
    if i == j:
        return 0
    elif j-i == 1:
        return A[i][0]*A[i][1]*A[j][1]
    else:
        if d[i][j]:
            return d[i][j]
        else:
            ret = sys.maxsize
            for k in range(i,j):
                tmp = calc(i,k)+calc(k+1,j)+A[i][0]*A[k][1]*A[j][1]
                if tmp < ret:
                    ret = tmp
            d[i][j] = ret
            return ret

n = int(input())
A = []
d = [[None for _ in range(n)] for _ in range(n)]
for i in range(n):
    a,b = [int(i) for i in input().split()]
    A.append((a,b))
print(calc(0,n-1))

Python: n = int(input())
p=[]
for i in range(n):
    s,c = map(int,input().split())
    if i == 0:
        p.append(s)
    p.append(c)


m = [[0 for _ in range(n+1)] for _ in range(n+1)]

for j in range(1, n):
    for i in range(1, n+1):
        if i+j > n:
            break
        if i != i+j:
            m[i][i+j] = min([m[i][k] + m[k+1][i+j] + p[i+j]*p[k]*p[i-1] for k in range(i,i+j)])

print(m[1][n])

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