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: #!/usr/bin/python3
n = int(input())
M = [tuple(list(map(int, input().split()))) for i in range(n)]

# best[i][i + j] : M_i * M_i+1 * ... * M_i+j?????????cost??¨??????
best = [[float('inf') for j in range(n)] for i in range(n)]
for j in range(0, n):
    for i in range(n - j):
        l = []
        for k in range(j):
            c1 = best[i][i + k]
            c2 = best[i + k + 1][i + j]
            c3 = M[i][0] * M[i + k][1]  * M[i + j][1]
            l.append(c1 + c2 + c3)
        best[i][i + j] = 0 if len(l) == 0 else min(l)
print(best[0][n-1])

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

INF = 1 << 60
dp = [[INF] * n for _ in range(n)]

for i in range(n):
    dp[i][i] = 0

for x in range(1, n):
    for i in range(n - x):
        j = i + x
        for k in range(i, j):
            dp[i][j] = min((dp[i][j], dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]))

print(dp[0][-1])

Python: def solve(a, b):
    global dp
    if a == b:
        return 0
    elif dp[a][b]:
        return dp[a][b]
    ret = INF
    for i in range(a, b):
        tmp = solve(a, i) + solve(i + 1, b) + c[i] * r[a] * c[b]
        ret = min(ret, tmp)
    dp[a][b] = ret
    return ret

n = int(input())
r = [0] * n;  c = [0] * n;
for i in range(n):
    r[i], c[i] = [int(x) for x in input().split()]

INF = float('inf')
dp = [[0 for _ in range(n)] for _ in range(n)]
print(solve(0, n - 1))

Python: import sys

n = int(input())

c = [0]*(n+1)
c[0],c[1] = map(int,input().split())

for i in range(2,n+1):
    c[i] = int(input().split()[1])

dp = [[sys.maxsize]*(n+1) for i in range(n+1)]

for i in range(1,n+1):
    dp[i][i] = 0

for l in range(2,n+1) :#length of chain j-i+1
    for i in range(1,n-l+2):
        j = i +l - 1
        for k in range(i,j):
            dp[i][j] = min(dp[i][j],dp[i][k]+ dp[k+1][j]+c[i-1]*c[k]*c[j])


print(dp[1][n])

Python: #区間[l,r]での最小値は?
#区間[l,l+mid]の最小値+[l+mid+1,r]の最小値+midに依存した計算回数
#(Mi Mj)*(Mj Mk)の計算回数はijk

def kuDP(l,r):
  global dp
  
  if dp[l][r] != -1:
    return dp[l][r]
  
  if l == r:
    dp[l][r] = 0
    return 0
  
  if l+1 == r:
    c = M[l][0]*M[l][1]*M[r][1]
    dp[l][r] = c
    return c
  
  midl = [10**10]*(r-l)
  for mid in range(r-l):
    midl[mid] = kuDP(l,l+mid) + kuDP(l+mid+1,r) + M[l][0]*M[l+mid][1]*M[r][1]
    
  c = min(midl)
  
  dp[l][r] = c
  
  return c

N = int(input())
M = [list(map(int,input().split())) for _ in range(N)]
dp = [[-1]*N for _ in range(N)]
print(kuDP(0,N-1))

Python: #!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def main():
    num_mat = int(stdin.readline())
    data = [int(s) for s in stdin.readline().split()]
    for _ in range(num_mat-1):
        _, row = (int(s) for s in stdin.readline().split())
        data.append(row)
    num_dat = len(data)

    bin_op = tuple(data[i]*data[i+1]*data[i+2] for i in range(num_dat-2))
    parts = [(data[0]*data[1], data[-2]*data[-1]), bin_op]

    for j in range(3, num_mat):
        part = parts[-1]
        new_part = []
        for i in range(num_dat-j):
            a = data[i]
            b = data[i+1]
            c = data[i+j-1]
            d = data[i+j]
            new_part.append(min(part[i] + a*c*d, part[i+1] + a*b*d))
        parts.append(new_part)

    L = data
    if num_mat < 2:
        print(0)
    elif num_mat < 3:
        print(data[0]*data[1]*data[2])
    else:
        print(min(parts[i][0] + parts[-(i+1)][-1] +
                  L[0]*L[i+1]*L[-1] for i in range(len(parts))))


main()

Python: n = int(input())
p0, p1 = list(map(int, input().split()))
p = [p0, p1]
for i in range(n-1):
    pi = list(map(int, input().split()))
    p.append(pi[1])
    
dp = [[10**10]*(n+2) for _ in range(n+2)]

for i in range(0, n+2):
    dp[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):
            dp[i][j] = min([dp[i][j], 
              dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]])

print(dp[1][n])

Python: #!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import re
import math
import collections
import itertools
import functools
import logging

logging.basicConfig(level=logging.DEBUG, format="DBG: %(message)s")

def dbg(msg, *args, **kwargs):
    logging.debug(msg, *args, **kwargs)

Mat = collections.namedtuple("Mat", "r c")

def mat_mul(ms):
    return Mat(ms[0].r, ms[-1].c)

def cost(m1, m2):
    return m1.r * m1.c * m2.c

def solve(n, ms):
    if n == 1: return 0

    dp = {}
    for i in range(n):
        dp[(i,i)] = 0

    for l in range(2, n+1):
        for i in range(n-l+1):
            cost_min = math.inf
            for j in range(l-1):
                dbg("l, i, j: %d %d %d", l, i, j)
                m_lhs = mat_mul(ms[i:i+j+1])
                m_rhs = mat_mul(ms[i+j+1:i+l])
                dbg("m_lhs, m_rhs: %r %r", m_lhs, m_rhs)
                cost_lhs = dp[(i,i+j)]
                cost_rhs = dp[(i+j+1,i+l-1)]
                dbg("cost_lhs, cost_rhs: %d %d", cost_lhs, cost_rhs)
                cost_mul = cost(m_lhs, m_rhs)
                dbg("cost_mul: %d", cost_mul)
                cost_now = cost_mul + cost_lhs + cost_rhs
                dbg("cost_now: %d", cost_now)
                if cost_min > cost_now:
                    cost_min = cost_now
            dp[(i,i+l-1)] = cost_min

    dbg(dp)
    return dp[(0,n-1)]

def main():
    n = int(input())

    ms = []
    for _ in range(n):
        r,c = map(int, input().split())
        ms.append(Mat(r,c))

    print(solve(n, ms))

if __name__ == "__main__": main()

Python: # -*- coding: utf-8 -*-
import functools
INF = 10**9
n = int(input())
p = list(map(int, input().split()))
for i in range(n - 1):
    p.append(int(input().split()[1]))


@functools.lru_cache(maxsize=None)
def matrix_chain_multiplication(i, j):
    if i == j:
        return 0

    tmp = INF
    for k in range(i, j):
        tmp = min(
            tmp,
            matrix_chain_multiplication(i, k) +
            matrix_chain_multiplication(k + 1, j) + p[i - 1] * p[k] * p[j])
    return tmp


print(matrix_chain_multiplication(0, n - 1))