Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list {
		buf.Write([]byte(fmt.Sprintf(" %d", v)))
	}
	buf.Write([]byte("\n"))
	buf.Flush()
}

//solve
//単純な場合
func matrixChainMultiplication() {
	//行列が1つだけのとき
	for i := 1; i <= n; i++ {
		m[i][i] = 0
	}
	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			m[i][j] = 1 << 21
			//kまでで区切る
			for k := i; k <= j-1; k++ {
				m[i][j] = min(m[i][j], m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j])
			}
		}
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

const MAX = 100

var n int
var p [MAX + 1]int

//m[i][j]: Mi * Mi+1 * ... * Mjを計算するときに必要なスカラー乗算の最小回数
var m [MAX + 1][MAX + 1]int

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 1; i <= n; i++ {

		p[i-1] = nextInt() //行数
		p[i] = nextInt()   //列数
	}

	matrixChainMultiplication()
	fmt.Println(m[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])

Python: import sys
import math
from collections import defaultdict, deque
from copy import deepcopy
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
def end_of_loop():raise StopIteration
    
    
def main():
    N = I()
    A = [0]*N
    B = [0]*N
    if N == 0:
        sys.exit()
    for i in range(N):
        a, b = MI()
        A[i] = a
        B[i] = b

    D = Init(N, N, 0)
    #tableの初期化
    for num1 in range(N-1):
        num2 = num1+1
        a,b,c = A[num1], B[num1], B[num2]
        D[num1][num2] = a*b*c
        D[num2][num1] = a*b*c

    for length in range(2, N):
        for num1 in range(N-length):
            num2 = num1+length
            #num1からnum2-1までの組み合わせで最小値を求める
            a = A[num1]
            c = B[num2]
            result = float('inf')
            for num3 in range(num1, num2):
                result = min(result, a*B[num3]*c + D[num1][num3] + D[num3+1][num2])
            D[num1][num2] = result
            D[num2][num1] = result
    print(D[0][N-1])
            
if __name__ == "__main__":
    main()

Python: # Matrix Chain Multiplication
N, *A = map(int, open(0).read().split())
A = A[0::2]+[A[-1]]

INF = float('inf')
dp = [[INF]*(N) for _ in [0]*(N)]
# dp[i][j]: a_iからa_jまでの積に必要な最小の乗算回数

rg = range(N)

for i in rg:
    dp[i][i] = 0


for l in range(1, N):
    for i in rg:
        j = i + l
        if j >= N:
            continue
        for k in range(i, j):
            dp[i][j] = min(
                dp[i][j],
                dp[i][k] + dp[k+1][j] + A[i]*A[k+1]*A[j+1]
            )

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)

Python: def min_cal(A):
    n=len(A)
    ls=[[0 for i in range(n)] for i in range(n)]
    for i in range(n):
        ls[i][i]=0
    for i in range(1,n):
        for j in range(n-i):
            ls[j][i+j]=min([ls[j][k]+ls[k+1][i+j]+A[k][1]*A[j][0]*A[i+j][1] for k in range(j,i+j)])
    return ls[0][n-1]

n=int(input())
Mats=[]
for i in range(n):
    Mats.append([int(i) for i in input().split()])

print(min_cal(Mats))

Python: def matrix_chain_multiplication(n, m, p):
    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])
    return m


n = int(input())
N = 100
p = [0 for i in range(N+1)]
m = [[0 for a in range(N+1)] for b in range(N+1)]

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

Python: # -*- coding: utf-8 -*-
import sys
INF=10**9
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()

def main():
    N=int(input())
    R,C=[0]*N,[0]*N
    A=[0]*(N+1)
    for i in range(N):
        A[i],A[i+1]=map(int,input().split())
    dp=[[INF]*N for _ in range(N)]
    dp[0]=[0]*N
    for i in range(N):
        for j in range(N-1-i):
            for k in range(i+1):
                dp[i+1][j]=min(dp[i+1][j],dp[i-k][j]+dp[k][i+1-k+j]+A[j]*A[j+i+1-k]*A[j+i+2])
    print(dp[-1][0])

if __name__ == '__main__':
    main()

Python: 
def mcm(p,n):
	m=[[0 for x in range(n+1)] for x in range(n+1)]
	for s in range(n):
		for i,j in zip(range(1,n+1),range(s+2,n+1)):
			m[i][j] = 1 << 30
			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])
	return m[1][n]

n=int(input())
p=[0]*(n+1)
for i in range(n-1):
	t=list(map(int,input().split()))
	p[i]=t[0];
t=list(map(int,input().split()))
p[n-1]=t[0]
p[n]=t[1]

ans=mcm(p,n)
print(ans)

Python: # http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_B&lang=ja

import sys
sys.setrecursionlimit(10**7)



def rec(l,r):
    if r-l < 1:
        return 0
    if r-l == 1:
        return M[l][0] * M[r][0] * M[r][1]
    if memo[l][r] != -1:
        return memo[l][r]
    memo[l][r] = min(rec(l, j-1) + rec(j, r) + M[l][0] * M[j-1][1] * M[r][1] for j in range(l+1,r+1))
    return memo[l][r]



N = int(input())
M = []
memo = [[-1]*N for _ in range(N)]

for i in range(N):
    M.append(list(map(int, input().split())))

print(rec(0,N-1))