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