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: import itertools
n = int(input())
rc = [list(map(int, input().split())) for _ in range(n)]
rc_min = rc.index(min(rc))

def calc(rc):
    total = 0
    for i, irc in enumerate(rc):
        if i ==0:
            r, c = irc
        else:
            total += r * c * irc[1]
            c = irc[1]
    return r, c, total
def reverse_calc(rc):
    total = 0
    for i, irc in enumerate(rc[::-1]):
        if i ==0:
            r, c = irc
        else:
            total += r * c * irc[0]
            r = irc[0]
    return r, c, total

total = 0
r1, c1, ret = reverse_calc(rc[:rc_min])
total += ret
r2, c2, ret =calc(rc[rc_min:])
total += ret
total += r1 * c1 * c2
print(total)

Python: def main():
    """連鎖行列積を求める.    
    """
    n = int(input())
    inf = 10**10
    if n == 0 or n == 1:
        print(0)
        return
    if n > 1:
        p = [int(input().split()[0]) for _ in range(n-1)]
    p.extend(list(map(int, input().split())))
    m = [[inf*(i!=j) for i in range(n)] for j in range(n)]
    for l in range(1, n):
        for i in range(n-l):
            j = i+l
            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][-1])

if __name__ == "__main__":
    main()

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

Python: n = int(input())
INF = 10**9
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 - 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][0] * P[k][1] * P[j][1])

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

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(1,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: import sys
 
n = int(input())
 
mlist = [0]*(n+1)
#print(mlist)
mlist[0],mlist[1] = map(int,input().split())
#print(mlist)
for i in range(2,n+1):
    mlist[i] = int(input().split()[1])
    #print(mlist)
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) :
    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]+mlist[i-1]*mlist[k]*mlist[j])
 
print(dp[1][n])

Python: import sys

def get_min_cost(n, p):
	m = [[0 for x in range(n+1)] for x in range(n+1)]
	for s in range(n):
		i = 1
		for j in range(s+2, n+1):
			m[i][j] = sys.maxsize
			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])
			i+=1	
	return m[1][n]

n = int(input())
p = [0] * (n+1)
j = 0
for i in range(n):
	a = list(map(int, input().split()))
	if i == 0:
		p[j] = a[0]
		j += 1
	p[j] = a[1]
	j += 1
print(get_min_cost(n, p))

Python: import functools
 
 
@functools.lru_cache(maxsize=None)
def rec(l, r):
    # [l, r)
    if l+1 == r:
        return 0
    ret = INF
    for mid in range(l+1, r):
        ret = min(ret, rec(l, mid) + rec(mid, r) + R[l] * R[mid] * C[r-1])
    return ret
 
INF = 10**9
N = int(input())
R, C = [], []
for i in range(N):
    r, c = map(int, input().split())
    R.append(r), C.append(c)
print(rec(0, 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 = [0]*(n+1)
m = [[None for _ in range(n+1)] for _ in range(n+1)]
for i in range(1,n+1):
    p[i-1],p[i] = map(int,input().split())
    m[i][i] = 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])