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: n = int(input())
m = []


def memoize(f):
    memo = [[None] * n for _ in range(n)]
    for i in range(n):
        memo[i][i] = 0

    def main(b, e):
        if memo[b][e] is not None:
            return memo[b][e]
        result = memo[b][e] = f(b, e)
        return result

    return main


@memoize
def contract(b, e):
    return min(contract(b, i) + contract(i + 1, e) + m[b] * m[i + 1] * m[e + 1] for i in range(b, e))


r, c = map(int, input().split())
m.append(r)
m.append(c)
nc = n - 1
while nc:
    r, c = map(int, input().split())
    m.append(c)
    nc -= 1

print(contract(0, n - 1))

Python: INF = float("inf")
n = int(input())
f = lambda x: list(map(int, x.split()))
A = [0]
for i, a in enumerate(map(f, sys.stdin)):
    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())
M=[list(map(int,input().split())) for _ in range(n)]

P=[M[0][0]]
for i in range(n):P.append(M[i][1])
DP=[[0 for _ in range(n)] for _ in range(n)]
for l in range(1,n):
    for i in range(n-l):
        j=i+l
        DP[i][j]=10**6
        for k in range(i,j):
            DP[i][j]=min(DP[i][j],DP[i][k]+DP[k+1][j]+P[i-1]*P[k]*P[j])
print(DP[0][-1])

Python: import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

N = int(input())
RC = [list(map(int, input().split())) for _ in range(N)]

memo = [[-1] * (N + 1) for _ in range(N + 1)]
inf = 10**18


def mult(l, r):
    """
    半開区間[l, r)
    """
    if memo[l][r] != -1:
        return memo[l][r]
    if r - l <= 1:
        memo[l][r] = 0
        return 0

    res = inf
    for m in range(l + 1, r):
        prod = RC[l][0] * RC[m - 1][1] * RC[r - 1][1]
        tmp = mult(l, m) + mult(m, r) + prod
        res = min(res, tmp)
    memo[l][r] = res
    return res


ans = mult(0, N)
print(ans)

Python: n = int(input())
p = [item for sublist in [[int(input().split()[1])] if i != 0 else list(map(int, input().split())) for i in range(n)] for item in sublist]

def memolize(f):
    cache = {}

    def helper(x, y):
        if (x, y) not in cache:
            cache[(x, y)] = f(x, y)
        return cache[(x, y)]
    return helper

@memolize
def calcMatrixOrder(i, j):
    if i == j :
        return 0
    return min([calcMatrixOrder(i, k) + calcMatrixOrder(k + 1, j) + p[i]*p[k+1]*p[j + 1] for k in range(i, j)])

print(calcMatrixOrder(0, n - 1))

Python: import sys
input = lambda: sys.stdin.readline().rstrip() 

def resolve():
    n = int(input())
    M = [list(map(int, input().split())) for _ in range(n)]

    # dp[i][j]: [i, j)の最小乗算数
    dp = [[0]*(n+1) for _ in range(n)]

    # 幅
    for i in range(2, n+1):
        # 開始位置
        for j in range(n-i+1):
            tmp = []
            # 切れ目
            for k in range(j+1, j+i):
                tmp.append(M[j][0]*M[k][0]*M[j+i-1][1]+dp[j][k]+dp[k][j+i])
            dp[j][j+i] = min(tmp)

    print(dp[0][n])

if __name__ == '__main__':
    resolve()

Python: # -*- coding: utf-8 -*-
N = int(input())
p = [0 for _ in range(N+1)]

p[0], p[1] = map(int, input().split())
for n in range(2, N+1):
    p[n] = list(map(int, input().split()))[1]

M = [[0]*(N+1) for _ in range(N+1)]

for n in range(N+1):
    M[n][n] = 0

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: # https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/10/ALDS1_10_B
# 普通に難しい
# アイデアは写真の解説を参照

# データの入力
N = int(input())

for i in range(N):
    if i == 0:
        P = list(map(int, input().split()))
        continue
    P.append(int(input().split()[1]))

# dpテーブルの用意 minを取るときはinf埋めしておくと処理が楽になるしバグが見つかりやすい
INF = 2**32-1
dp = [[INF]*N for _ in range(N)]

# dpテーブルの初期化
# 対角成分0
for ij in range(N):
    dp[ij][ij] = 0

# dpの更新 どう更新しているのか、なんでこう更新するのかは写真を参照
for l in range(1, N):  # lは対角成分からいくつ離れているか (なん個行列をかけるかという解釈も可)
    for i in range(N):
        j = i+l
        if j >= N:
            continue
        for k in range(i, j):  # 最小値探索
            dp[i][j] = min(dp[i][k] + dp[k+1][j] +
                           P[i] * P[k+1] * P[j+1], dp[i][j])
print(dp[0][-1])

Python: import sys
def input():
    return sys.stdin.readline()[:-1]
n = int(input())
A = [list(map(int, input().split())) for _ in range(n)]

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

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):
            x = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]
            if not 0<m[i][j]<=x:
                m[i][j] = x
print(m[1][n])