Go: package main

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

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	fmt.Println(resolve(parseProblem()))
}

type matrixSize struct{ row, col int }

func calcNum(i, j matrixSize) int {
	return i.row * j.col * i.col
}

func parseProblem() (int, []matrixSize) {
	n := scanInt(sc)
	sizes := make([]matrixSize, n)
	for i := 0; i < n; i++ {
		sizes[i] = matrixSize{row: scanInt(sc), col: scanInt(sc)}
	}
	return n, sizes
}

var dp [][]int

func initDP(n int) {
	dp = make([][]int, n+1)
	for i := 0; i < len(dp); i++ {
		dp[i] = make([]int, n+1)
		for j := 0; j < len(dp[i]); j++ {
			dp[i][j] = math.MaxInt64
		}
	}
}

func resolve(n int, sizes []matrixSize) int {
	// TODO 復習
	initDP(n)
	_rec(sizes, 0, n)
	return dp[0][n]
}

func _rec(sizes []matrixSize, l, r int) int {
	if dp[l][r] != math.MaxInt64 {
		return dp[l][r]
	}
	if r-l <= 1 {
		dp[l][r] = 0
		return 0
	}
	num := math.MaxInt64
	for i := l + 1; i < r; i++ {
		v := _rec(sizes, l, i) + _rec(sizes, i, r) + calcNum(matrixSize{sizes[l].row, sizes[i].row}, matrixSize{sizes[i].row, sizes[r-1].col})
		if v < num {
			num = v
		}
	}
	dp[l][r] = num
	return dp[l][r]
}

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: import math
n = int(input())
p1 = []
p = []
m = [[] for i in range(n)]
for i in range (n):
    p1.append(list(map(int, input().split())))
    p.append(int(p1[i][0]))
    for j in range(n):
        m[i].append(0)

p.append(int(p1[i][1]))

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

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

Python: def get_multi_num(M):
    D = {}
    def get_multi_num_core(M):
        if len(M) < 2: return 0
        key = tuple(M)
        if key in D: return D[key]
        result = [None]*(len(M)-1)
        for i in range(1,len(M)):
            result[i-1] = get_multi_num_core(M[:i]) + \
                          get_multi_num_core(M[i:]) + \
                          M[0][0]*M[i][0]*M[-1][1]
        D[key] = min(result)
        return D[key]
    return get_multi_num_core(M)
        
if __name__=='__main__':
    n = int(input())
    M = [None]*n
    for i in range(n):
        M[i] = tuple(map(int,input().split()))
    print(get_multi_num(M))

Python: import math
import copy
import sys
import fractions
# import numpy as np
# import statistics
import decimal
import heapq
import collections
import itertools
import bisect

# from operator import mul

sys.setrecursionlimit(100001)


# input = sys.stdin.readline
# sys.setrecursionlimit(10**6)

# ===FUNCTION===

def getInputIntList():
    outputDataList = []
    inputData = input().split()
    outputDataList = [int(n) for n in inputData]

    return outputDataList


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList


# ===CODE===


n = int(input())

data_set = getSomeInputListInt(n)
data_set.insert(0, 0)

INF = sys.maxsize

dp = [[INF for _ in range(n+1)] for _ in range(n+1)]
for i in range(1, n+1):
    dp[i][i] = 0
next_dp = copy.deepcopy(dp)

# ans = INF
# for s in range(1, n):
#     for i in range(1, n+1):
#         for j in range(i+s, n+1):
#             for k in range(i, j+1):
#                 if k >
#                 if dp[i][k] < INF and dp[k+1][j] < INF:
#                     next_dp[i][j] = min(next_dp[i][j], dp[i][k]+dp[k+1][j]+data_set[i][0]*data_set[k][1]*data_set[j][0])
#
#     dp = next_dp
#     ans = min(ans, dp[1][n])
# print(ans)


for s in range(1, n):
    for i in range(1, n+1):
        j = i+s
        if j > n:
            break
        for k in range(i, j):
            if dp[i][k] < INF and dp[k+1][j] < INF:
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + data_set[i][0] * data_set[k][1] * data_set[j][1])

print(dp[1][n])

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

for i in range(n):
    p[i], p[i+1] = map(int, input().split())
     
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])

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: inf = float('inf')

n = int(input())

r = [0] * n         # 行数
c = [0] * n         # 列数

for i in range(n):
    ri, ci = map(int, input().split())
    r[i] = ri
    c[i] = ci

dp = [[0]*(n+1) for _ in range(n+1)]  # (n+1)*(n+1)のゼロ行列生成

for ls in range(2, n+1):        # 対象とする行列の数をlsとした。
    for i in range(n+1-ls):
        dp[i][i+ls] = inf
        for j in range(i+1, i+ls):  # 働きとしてはkに該当？
            dp[i][i+ls] = min(dp[i][i+ls], dp[i][j] + dp[j][i+ls] + r[i]*r[j]*c[i+ls-1])

ans = dp[0][n]

print(ans)

Python: import sys
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 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: n = int(input())
M = []
dp = [[None for i in range(100)] for i in range(100)]

def calcMatrix(i,j):
    #print(i,j)
    if i == j:
        dp[i][j] = 0
    elif i == j-1:
        dp[i][j] = M[i]*M[i+1]*M[i+2]
    else:
        for k in range(j-i):
            #print(i,j,k)
            #print(dp[i][i+k])
            #print(dp[i+k+1][j])
            #print(M[i]*M[k+1]*M[j])
            current = dp[i][i+k] + dp[i+k+1][j] + M[i]*M[i+k+1]*M[j+1]
            if k==0:
                dp[i][j] = current
            if current < dp[i][j]:
                dp[i][j] = current 

for i in range(n-1):
    Q = list(map(int,input().split()))
    M.append(Q[0])
Q = list(map(int,input().split()))
M.append(Q[0])
M.append(Q[1])
#print(M)

for i in range(n):
    for j in range(n-i):
        calcMatrix(j,i+j)
        #for k in range(10):
        # print(dp[k])
        # print()
print(dp[0][n-1])

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 i in range(n+1)] for j in range(n+1)]

for i in range(2, n+1): #幅
    for j in range(0, n+1-i): #左端
        dp[j][j+i] = min([dp[j][k]+(r[j]*c[k-1]*c[j+i-1])+dp[k][j+i] for k in range(j+1,j+i)])

print(dp[0][n])