Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	p := make([]int, N+1)
	dp := make([][]int, N+1)
	for i, _ := range dp {
		dp[i] = make([]int, N+1)
	}
	for i := 0; i < N; i++ {
		p[i] = sc.nextInt()
		p[i+1] = sc.nextInt()
	}
	for i := 1; i <= N; i++ {
		dp[i][i] = 0
	}
	for l := 2; l <= N; l++ {
		for i := 1; i <= N-l+1; i++ {
			j := i + l - 1
			dp[i][j] = 1 << 60
			for k := i; k <= j-1; k++ {
				dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}

	fmt.Fprintln(wtr, dp[1][N])
	wtr.Flush()
}

Python: def main():
    n = int(input())
    p = [0] * (n + 1)
    m = [[0] * (n + 1) for _ in range(n + 1)]
    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):
                p_prod = p[i - 1] * p[k] * p[j]
                m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p_prod)
    print(m[1][n])


if __name__ == '__main__':
    main()

Python: def matrixChainMultiplication():
	INF = 100 * 100 * 100 + 1
	m = [[INF for _ in range(n + 1)] for _ 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):
				q = m[i][k] + m[k+1][j] + p[i-1] * p[k] * p[j]
				if q < m[i][j]:
					m[i][j] = q
	return m[1][n]

if __name__ == "__main__":
	n = int(input())
	p = list(map(int, input().split()))
	for i in range(1, n):
		p.append(list(map(int, input().split()))[1])
	print(matrixChainMultiplication())

Python: INF = float("inf")
n = int(input())
A = [0]
for i, a in enumerate(sys.stdin):
    a = list(map(int, 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: 
from functools import lru_cache


@lru_cache(maxsize=None)
def multi(ms):
    """Matix multiplication. returns minimum number of scalar
    multiplications to compute all matrix multiplications.

    >>> multi(((30, 35), (35, 15), (15, 5), (5, 10), (10, 20), (20, 25)))
    15125
    """
    size = len(ms)
    if size <= 1:
        return 0
    elif size == 2:
        r1, c1 = ms[0]
        r2, c2 = ms[1]
        return c1 * r1 * c2

    costs = []
    for i in range(size-1):
        cost = ms[i][1] * ms[0][0] * ms[-1][1]
        costs.append(multi(ms[:i+1]) + multi(ms[i+1:]) + cost)

    return min(costs)


def run():
    n = int(input())
    ms = []

    for i in range(n):
        m = tuple([int(x) for x in input().split()])
        ms.append(m)

    print(multi(tuple(ms)))


if __name__ == '__main__':
    run()

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: import sys
INF = float("inf")
n = int(input())
A = [0]
for i, a in enumerate(sys.stdin):
    a = list(map(int, 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: # Solution to this problem using memoization
# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_10_B

import math

# Optimal cost of multiplying A_1 A_2 ... A_j
def cost(i, j):
    if (i, j) in cost_dict.keys():
        return cost_dict[(i, j)]
    elif i == j:
        cost_dict[(i, j)] = 0
    else:
        mincost = math.inf
        for k in range(i, j):
            estimate = cost(i, k) + cost(k + 1, j) + dims[i - 1] * dims[k] * dims[j]
            mincost = min(estimate, mincost)
        cost_dict[(i, j)] = mincost
    return cost_dict[(i, j)]


if __name__ == '__main__':
    # Number of matrices
    n = int(input())
    # A dictionary to store costs
    cost_dict = dict()
    # Take input dimensions
    dims = []
    for _ in range(n - 1):
        first = int(input().split()[0])
        dims.append(first)
    dims.extend(list(map(int, input().split())))
    # Calculate the optimal cost
    print(cost(1, 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 1>m[r][c]or m[r][c]>x:m[r][c]=x
 print(m[r][c])
if'__main__'==__name__:s()

Python: n = int(input())
p = list(range(n+2))

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

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

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


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