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: # Matrix Chain Multiplication
import sys

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

dp = [[0 for _ in range(N + 1)] for _ in range(N + 1)]
for l in range(2, N + 1):
    for i in range(1, N - l + 2):
        j = i + l - 1
        dp[i][j] = sys.maxsize
        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: 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 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])
if'__main__'==__name__:s()

Python: n = int(input())
mat = []
for _ in range(n):
    mat.append(list(map(int, input().split())))
inf = int(1e11 + 10)
dp = [[inf]*(n + 1) for _ in range(n + 1)]
for i in range(n):
    dp[i][i] = 0
for l in range(2, n + 1):
    for i in range(n):
        j = i + l - 1
        if i + l > n:
            break
        for k in range(i, j):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + mat[i][0]*mat[k][1]*mat[j][1])
print(dp[0][n - 1])

Python: class Mychain:
    # 名前はこれでいいんだろうか・・・

    def __init__(self, n):
        self.n = n
        self.lst = [0 for _ in range(n * (n + 1) // 2)]

    def get_index(self, i, j):
        return i - 1 + ((j - i) * (2*self.n - j + i + 1) // 2)

    def get_value(self, i, j):
        return self.lst[self.get_index(i, j)]

    def set_value(self, i, j, value):
        index = self.get_index(i, j)
        self.lst[self.get_index(i, j)] = value


n = int(input())
res = Mychain(n)
matrix = [int(i) for i in input().split()]
matrix.extend([int(input().split()[1]) for i in range(n - 1)])


for n1 in range(1, n + 1):
    for i in range(1, n - n1 + 1):
        j = n1 + i
        value = min(res.get_value(i, i + k) + res.get_value(i + k + 1, j)
                    + matrix[i - 1] * matrix[i + k] * matrix[j]
                    for k in range(n1))
        res.set_value(i, j, value)

print(res.get_value(1, n))

Python: n = int(input())

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


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

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

m = [[0] * n for i in range(n)]

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

print(m[0][n - 1])

Python: import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

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

dp = [[INF] * (n + 10) for _ in range(n + 10)]

"""
dp[i][j] = iからj番目の行列積に必要なスカラー積の最小値
dp[i][j] = min(dp[i][k] + dp[k+1][j] + M[i].row * M[k].col * M[j].col)
"""

for i in range(n):  # 初期条件
    dp[i][i] = 0

for i in range(n, -1, -1):
    for j in range(i+1, n):
        for k in range(i, j):
            prod = row[i] * column[k] * column[j]
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + prod)
print(dp[0][n-1])

Python: """
n = input().split()

n = input()
nele = int(n[0])
list = range(1,nele+1)

nrow = {}
ncol = {}
for i in list:
	nrow[i] = int(n[i*2-1])
	ncol[i] = int(n[i*2])
"""

n = input()
nele = int(n[0])
list = range(1,nele+1)
nrow = {}
ncol = {}
for i in list:
	n = input().split()
	nrow[i] = int(n[0])
	ncol[i] = int(n[1])


#print(nrow, ncol)


min = -1
cur = -1
route = []
done = []

for i in list:
	route.append(0)

#print(route)

depth = -1
min = -1
def tansaku(prev):
	
	global depth
	depth += 1
	
	global nrow
	global ncol
	global min
	global route
	
	done = route[:]
	
	for i in list:
#		print(depth, i, route, done)
		
		if i in done:
			continue
		
		next = i
		done.append(next)
		
		if depth == 0:
			route[depth] = next
			tmp = 0
			min = tansaku(tmp)
			continue
		
		tmp = prev + nrow[route[depth-1]] * ncol[next]
#		print(tmp)
		
		if tmp > min and min != -1:
#			print("aaa")
			continue
		else:
			route[depth] = next
			if depth >= nele-1:
#				print("bbb")
				min = tmp
			else:
#				print("ccc")
				min = tansaku(tmp)
	
	depth -= 1
	route[depth] = 0
#	print("min:", min)
	return min

min = tansaku(0)

print(min)

Python: n = int(input())
INF = 10**9
P = []
for i in range(n):
    p = map(int, input().split())
    P.append()
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1][0] * p[k][1] * p[j][1])

print(m[0][n - 1])