Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func stringifyArray(arr []int) string {
	return strings.TrimRight(fmt.Sprintf("%+v", arr)[1:], "]")
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	m := make([][]int, n)
	sizes := make([][]int, n)
	for i := 0; i < n; i++ {
		sizes[i] = []int{nextInt(), nextInt()}
		m[i] = make([]int, n)
	}
	// fmt.Println(sizes)

	for w := 1; w < n; w++ {
		for i := 0; i < n-w; i++ {
			j := i + w
			minBetweenIandJ := 10000000
			for k := i; k < j; k++ {
				// fmt.Println(i, j, k, m[i][k], m[k+1][j], sizes[i][0]*sizes[k][1]*sizes[j][1])
				minBetweenIandJ = min(minBetweenIandJ, m[i][k]+m[k+1][j]+sizes[i][0]*sizes[k][1]*sizes[j][1])
			}
			m[i][j] = minBetweenIandJ
		}
	}
	// for i := 0; i < len(m); i++ {
	// 	fmt.Println(stringifyArray(m[i]))
	// }
	fmt.Println(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(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: #!/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 > 1:
        print(min(parts[i][0] + parts[-(i+1)][-1] +
                  L[0]*L[i+1]*L[-1] for i in range(len(parts))))
    else:
        print(0)


main()

Python: #coding:utf-8
n = int(input())
m = [[0 for i in range(n)] for j in range(n)]
p = []
for i in range(n):
    p1,p2 = map(int,input().split())
    if i == 0:
        p.append(p1)
    p.append(p2)

def matrixChainMultiplication():
    for i in range(n):
        m[i][i] = 0

    for l in range(2,n+1):
        for i in range(0,n-l+1):
            j = i + l - 1
            m[i][j] = 1000000
            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])
matrixChainMultiplication()
print(m[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, row_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
			row_next = nrow[next]
			min = tansaku(tmp, row_next)
			continue
#		print(route, route[depth-1], next, nrow[route[depth-1]], ncol[next], prev)
#		tmp = prev + nrow[route[depth-1]] * ncol[next]
		print(prev, row_prev, ncol[next])
#		tmp = prev + row_prev * ncol[next]
		row_next = ncol[next]
#		print(depth, 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, row_next)
	
	depth -= 1
	route[depth] = 0
	print("min:", min)
	return min

min = tansaku(0, 0)

print(min)

Python: # Dynamic Programming - Matrix Chain Multiplication
INFTY = float('inf')
MAX = 200
size = int(input())
# 1???????????§??????
C = [[INFTY for _ in range(MAX+1)] for i in range(MAX+1)]
R = [0 for i in range(MAX+2)]
for i in range(size):
    a,b = map(int, input().split())
    if i == 0:
        R[1] = a
    R[i+2] = b

for i in range(1, size+1): C[i][i] = 0

for j in range(1,size):
    for i in range(1,size):
        for k in range(i+1,i+j+1):
            cost = C[i][k-1] + C[k][i+j] + R[i]*R[k]*R[i+j+1]
            if cost < C[i][i+j]:
                C[i][i+j] = cost
print(C[1][size])

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 not 0<m[r][c]<x:m[r][c]=x
 print(m[r][c])
if'__main__'==__name__:s()

Python: MAX = 10 ** 8
n = int(input())
M = [list(map(int,input().split())) for _ in range(n)]
dp = [[MAX for _ in range(n)] for _ in range(n)]
r = [0 for _ in range(n)] 
c = [0 for _ in range(n)] 

for i in range(n):
    r[i] = M[i][0]
    c[i] = M[i][1]
    dp[i][i] = 0

for d in range(1,n):
    for i in range(n-d):
        j = i + d
        min_v = MAX
        for k in range(i,j):
            v = r[i] * c[k] * c[j] + dp[i][k] + dp[k+1][j]
            min_v = min(min_v,v)

        dp[i][j] = min(dp[i][j], min_v)

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

Python: INFTY = float('inf')

n = int(input())
M = [[0 for i in range(n)] for i in range(n)]
p = []

for i in range(n):
    r, c = list(map(int, input().split(' ')))
    if i == 0: p.append(r)
    p.append(c)

for i in range(n-1):
    M[i][i+1] = p[i]*p[i+1]*p[i+2]

for gap in range(2,n): # gap = 1,.., n-1
    for i in range(n-gap):
        j = i+gap
        M[i][j] = INFTY
        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=[0]*(n+1)
for i in range(n):
    p[i],p[i+1]=map(int,input().split())

dp=[[10**10]*(n) for i in range(n)]    
#dp=[[0]*(n+1) for i in range(n+1)]    
    
#dp[0][1]=p[0]*p[1]*p[2]
dp[0][0]=0
for j in range(1,n):
    for i in range(j,-1,-1):
#        print("-------------i:",i,"j:",j)
        if j==i:
            dp[i][j]=0
#            print("i:",i,"j:",j,"k:",999)
        else:
#            for k in range(i+1,j):
            for k in range(j-1,i-1,-1):
#                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j])
                dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i]*p[k+1]*p[j+1])
#                print("i:",i,"j:",j,"k:",k)
#                print(dp[i][j],dp[i][k],dp[k+1][j],p[i-1]*p[k]+p[j])
print(dp[0][n-1])

Python: def matrixChainMultiplication(p):
    # p = [p_0, p_1, ..., p_n]
    n = len(p) - 1
    # n+1 x n+1 zero matrix?????????(0,...,n)
    # cost(i,j): (M_i...M_j)???????°??????????
    cost = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
    for numOfMatrices in range(2, n + 1):
        for i in range(1, n - numOfMatrices + 2):
            j = i + numOfMatrices - 1
            cost[i][j] = float('inf')
            for k in range(i, j):
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k + 1][j] + p[i - 1] * p[k] * p[j])
    print(m[1][n])

p = []
n = int(input())

for _ in range(n):
    listed_input = input().split()
    p.append(int(listed_input[0]))
p.append(int(listed_input[1]))

matrixChainMultiplication(p)