Go: package main

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

type Matrix struct {
	r int // num of row
	c int // num of column
}

type Range struct {
	start int // index, inclusive
	end   int // index, inclusive
}

func calcMultiplication(x, y Matrix) (Matrix, int) {
	if x.c != y.r {
		panic("cannnot calculate")
	}
	return Matrix{x.r, y.c}, x.r * y.c * x.c
}

func calcMinMatrixChainMultiplication(matrices []Matrix, r Range, memo map[Range]int) (Matrix, int) {
	if v, ok := memo[r]; ok {
		return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, v
	}
	if len(matrices) == 0 {
		panic("must not be empty")
	} else if len(matrices) == 1 {
		return matrices[0], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < len(matrices)-1; i++ {
		formerMatrices := matrices[:i+1]
		latterMatrices := matrices[i+1:]
		matF, countF := calcMinMatrixChainMultiplication(formerMatrices, Range{0, i}, memo)
		matL, countL := calcMinMatrixChainMultiplication(latterMatrices, Range{i + 1, len(matrices) - 1}, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[0].r, matrices[len(matrices)-1].c}, minCount
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, _ := nextInt(sc)
	matrices := make([]Matrix, n)
	for i := 0; i < n; i++ {
		r, _ := nextInt(sc)
		c, _ := nextInt(sc)
		matrices[i] = Matrix{r, c}
	}
	memo := make(map[Range]int)
	_, resultCount := calcMinMatrixChainMultiplication(matrices, Range{0, len(matrices) - 1}, memo)
	fmt.Println(resultCount)
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

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

Python: from sys import stdin
from collections import deque
inf=float('inf')
def search(A,N):
    dp=[[inf for x in range(N+1)] for j in range(N+1)]

    p=[]
    for i in range(N+1):
        dp[i][i]=0
        if i>=1:
            if i==1:
                p.append(A[i][0])
                p.append(A[i][1])
            else:p.append(A[i][1])
    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]+p[i-1]*p[k]*p[j])
    return dp[1][-1]

N=int(input())
num_list = [[0, 0]]
for i in range(N):
    num_list.append(list(map(int,stdin.readline().strip().split())))
print(search(num_list,N))

Python: import sys

def mcm(sizes, memo, l, r):
    if l == r:
#        print('a:', l, r)
        return 0
    if memo[l][r] >= 0:
#        print('b:', l, r)
        return memo[l][r]
#    print('c:', l, r)
    m = sys.maxsize
    for k in range(l, r):
        m = min (m, mcm(sizes, memo, l, k) + mcm(sizes, memo, k+1, r) + sizes[l][0] * sizes[k][1] * sizes[r][1])
    memo[l][r] = m
    return memo[l][r]

if __name__ == '__main__':
    # Load input
    n = int(input())
    x = []
    for _ in range(n):
        y = tuple(map(int, input().split(' ')))
        x.append(y)
    
    # Create memo:
    memo = []
    for _ in range(n):
        memo.append([-1]*n)
#    print(x)
    print( mcm(sizes = x, memo=memo, l = 0, r = 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()