Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

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

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(m[0][n-1])
}

Python: inf = 10**9+7
mod = 10**9+7
n = int(input())
rc = [list(map(int, input().split())) for i in range(n)]
dp = [[inf for j in range(n)]for i in range(n)]
for i in range(n):
    dp[i][i] = 0
for k in range(1,n):
    for i in range(0,n-k):
        for j in range(k):
            dp[i][i+k] = min(dp[i][i+k], dp[i][i+j] + dp[i+j+1][i+k] + rc[i][0]*rc[i+j][1]*rc[i+k][1])
print(dp[0][n-1])

Python: def matrix_chain_multiplication(n):
    m = [[0 for x in range(n+1)] for y in range(n+1)]
    for l in range(2, n+1):
        for i in range(1, n-l+2):
            j = i + l - 1
            m[i][j] = float('inf')
            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])

p = []
n = int(input())

for i in range(n):
    listed_input = input().split()
    p.append(int(listed_input[0]))

p.append(int(listed_input[1]))

matrix_chain_multiplication(n)

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(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 not 0<m[r][c]<=x:m[r][c]=x
 print(m[r][c])
if'__main__'==__name__:s()

Python: import sys


input = sys.stdin.readline
n = int(input())
m_list = []
for _ in range(n):
    m_list.append(list(map(int, input().split())))

p_list = [m_list[0][0]]
for i in range(n):
    p_list.append(m_list[i][1])

m = [[10**9]*(n+1) for i in range(n+1)]

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
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p_list[i-1]*p_list[k]*p_list[j])

print(m[1][n])

Python: # coding: utf-8
# Your code here!
INF = 2000000000

def matrixChainMultiplication(m):
    global n
    for l in range(2, n+1):
        for i in range(1, n-l + 2):
            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-1]*p[k]*p[j])
    return m
 
    
n = int(input())

m = [[0 for i in range(n + 1)] for j in range(n + 1)]
p = []


for i in range(n):
    input_command = input()
    num1,num2 = input_command.split()
    p.append(int(num1))
    if i == n-1:
        p.append(int(num2))
        
m = matrixChainMultiplication(m)
print(m[1][n])

Python: n = int(input())
p = []
a,b = [int(x) for x in input().split()]
p.append(a)
p.append(b)
for i in range(1, n):
    c,d = [int(x) for x in input().split()]
    p.append(d)
# end input
# create dp table
dp = []
for x in range(n+1):
    dp.append([0] * (n+1))

for l in range(2, n+1):
    for i in range(1, n-l+2):
        j = i + l -1
        dp[i][j] = 9999999999

        for k in range(i, j):
            tmp = dp[i][k] + dp[k+1][j] + (p[i-1] * p[k] * p[j])
            if dp[i][j] > tmp:
                dp[i][j] = tmp
#print(dp)
print(dp[1][n])

Python: import functools


@functools.lru_cache(maxsize=None)
def rec(l, r):
    # [l, r)
    if l+1 == r:
        return 0
    ret = INF
    for mid in range(l+1, r):
        ret = min(ret, rec(l, mid) + rec(mid, r) + R[l] * R[mid] * C[r-1])
    return ret

INF = 10**9
N = int(input())
R, C = [], []
for i in range(N):
    r, c = map(int, input().split())
    R.append(r), C.append(c)
print(rec(0, N))

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(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 not 0<m[r][c]<=x:m[r][c]=x
 print(m[r][c])
if'__main__'==__name__:s()

Python: def divide_list(l):
    divided_list = [[None, None] for _ in range(len(l) - 1)]
    for i, dl in enumerate(divided_list):
        dl[0], dl[1] = l[0:(i+1)], l[(i+1):]
    return divided_list


def mat_chain_mult(idx_list):
    global a
    global c

    start, end = idx_list[0], idx_list[-1]


    if c[start][end] is not None:
        return c[start][end]

    if len(idx_list) == 1:
        c[start][end] = 0
    elif len(idx_list) == 2:        
        c[start][end] = a[start] * a[start+1] * a[start+2]
    else:
        divided_list = divide_list(idx_list)
        cc = a[start] * a[end+1]
        
        c[start][end] = min([mat_chain_mult(dl0) + mat_chain_mult(dl1) + cc * a[len(dl0) + start] for dl0, dl1 in divided_list])
    
    return c[start][end]
    

if __name__ == "__main__":
    n = int(input())
    a = [0] * (n + 1)
    c = [[None] * n for _ in range(n)]

    for i in range(n-1):
        a[i] = list(map(int, input().split()))[0]
    a[n-1], a[n] = list(map(int, input().split()))

    print(mat_chain_mult(list(range(n))))

Python: INF = float("inf")
n = int(input())
A = [0]
for i, a in enumerate(sys.stdin):
    a = list(map, a.split())
    if i == 0:
        A.extend(a)
    else:
        A.append(a[1])
C = [[INF] * (n + 1) for _ in range(n + 1)]
for i in range(n + 1):
    C[i][i] = 0
for j in range(1, n):
    for i in range(1, n - j + 1):
        for k in range(i + 1, i + j + 1):
            cost = C[i][k - 1] + C[k][i + j] + A[i] * A[k] * A[i + j + 1]
            if cost < C[i][i + j]:
                C[i][i + j] = cost
print(C[1][n])

Python: 
n = int(input())
p = [None] * (n + 1)
m = [[None for i in range(n+1)] for j in range(n+1)]

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

# print(p)
# print(m)

for l in range(2, n+1):  # lはchainの長さ。最終的にl = n。
    for i in range(1, n-l+2):  # i = チェーンの左側
        j = i + l - 1 # j = チェーンの右端
        m[i][j] = 1000000000

        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())
p = list(range(n+1))

for i in range(n):
    p[i], p[n] = map(int, input().split())

# table[i][j] : M_i??????M_j??????????????????????°?????????°
table = [[0] * n for i in range(n)]

# ??£????????£???2????????????????????????
for i in range(n-1):
    table[i][i+1] = p[i]*p[i+1]*p[i+2]


for k in range(3, n+1):    # ?????????k???????????????????????????
    for i in range(n+1-k): # M_i?????????
        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<=k)
            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])