Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	mm := make([]int, n+1)
	dp := make([][]int, n+1)
	for i := 0; i <= n; i++ {
		dp[i] = make([]int, n+1)
	}
	for i := 0; i < n; i++ {
		mm[i] = getNextInt(scanner)
		mm[i+1] = getNextInt(scanner)
	}

	for l := 2; l <= n; l++ {
		for i := 1; i <= n-l+1; i++ {
			j := i + l - 1
			for k := i; k <= j-1; k++ {
				d := dp[i][k] + dp[k+1][j] + mm[i-1]*mm[k]*mm[j]
				if dp[i][j] == 0 || dp[i][j] > d {
					dp[i][j] = d
				}
			}
		}
	}

	fmt.Fprintln(writer, dp[1][n])

	writer.Flush()
}

Python: # coding: utf-8

INF = 1 << 60
n = int(input().rstrip())
r = [0 for _ in range(n+10)]
c = [0 for _ in range(n+10)]
# i番目の行列からj番目の行列までの最小値
dp = [[INF for _ in range(n+10)] for _ in range(n+10)]
for i in range(n):
    line = input().rstrip().split()
    r[i] = int(line[0])
    c[i] = int(line[1])
    
for i in range(n):
    dp[i][i] = 0

# 行列の距離が近い方から計算しなくてはいけない！
for l in range(1,n):
    for i in range(n - l):
        j = i + l
        for k in range(i,j):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + r[i]*c[k]*c[j])
            
print(dp[0][n-1])

Python: n = int(input())
m = [[0] * (n + 1) for _ in range(n + 1)]
z = [0] * (n + 1)
for i in range(n):
    z[i], z[i + 1] = map(int, input().split())
j = 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] + z[i - 1] * z[k] * z[j])
print(m[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 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])
s()

Python: # [New] Matrix Chain Multiplication using Dynamic Programming Formula
# https://www.youtube.com/watch?v=_WncuhSJZyA

n = int(input())

P = []
a, b = [int(i) for i in input().split()]
P.append(a)
P.append(b)
for _ in range(n - 1):
    _, b = [int(i) for i in input().split()]
    P.append(b)

dp = [[None] * (n + 1) for _ in range(n + 1)]  # 1-indexed table

for i in range(1, n + 1):
    dp[i][i] = 0

for r in range(2, n + 1):
    for i in range(1, n - r + 2):
        j = i + r - 1
        dp[i][j] = float("inf")
        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][-1])

Python: n=int(input())
a= [list(map(int, input().split())) for i in range(n)]
a+=a
# dp[i][j]=iからjまでの区間連鎖行列積
INF=10**12
dp=[[INF]*(2*n+1) for i in range(2*n+1)]
ans=0
for i in range(n):
    for l in range(2*n-i):
        r=i+l
        if l==r:
            dp[l][r]=0
        else:
            for k in range(l,r):
                dp[l][r]=min(dp[l][k]+dp[k+1][r]+a[l][0]*a[k][1]*a[r][1],dp[l][r])


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

Python: # -*- coding: utf-8 -*-


if __name__ == '__main__':

    n = int(input())
    p = [0]
    for _ in range(n):
        r, c = list(map(int, input().split(" ")))
        p[-1] = r
        p.append(c)
    m = [[0 for i in range(n+1)] for j in range(n+1)]
    
    for i in range(1, n+1):
        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])

C++: #include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
int dp[111][111],n,L[111],R[111];
int f(int l,int r){
	if(dp[l][r]>-1)return dp[l][r];
	int res=1e9;
	for(int i=l;i<r;i++){
		res=min(res,f(l,i)+f(i+1,r)+L[l]*R[i]*R[r]);
	}
	return dp[l][r]=res;
}
main(){
	memset(dp,-1,sizeof(dp));
	r(i,111)dp[i][i]=0;
	cin>>n;
	r(i,n)cin>>L[i]>>R[i];
	cout<<f(0,n-1)<<endl;
}

C++: #include <algorithm>
#include <iostream>
#include <list>

struct Matrix
{
    int r;
    int c;
};
int Multiple(std::list<Matrix>::iterator Left)
{
    std::list<Matrix>::iterator Right = Left; Right++;
    int Count = Left->r * Right->c * Left->c;
    Right->r = Left->r;
    return Count;
}
bool CompareR(const Matrix& Left, const Matrix& Right)
{
    return Left.r < Right.r;
}
bool CompareC(const Matrix& Left, const Matrix& Right)
{
    return Left.c < Right.c;
}
int Mcm(const std::list<Matrix>& AList)
{
    std::list<Matrix> List = AList;
    const std::list<Matrix>::iterator MinR = std::min_element(List.begin(), List.end(), CompareR);
    const std::list<Matrix>::iterator MinC = std::min_element(List.begin(), List.end(), CompareC);
    int Count = 0;
    for(std::list<Matrix>::iterator It = MinC; It != List.begin(); It){
        --It;
        Count += Multiple(It);
        It = List.erase(It);
    }
    for(std::list<Matrix>::iterator It = MinR; It != --List.end(); It){
        Count += Multiple(It);
        It = List.erase(It);
    }
    Count += Multiple(List.begin());
    return Count;
}

int main()
{
    std::list<Matrix> List;
    int n;
    std::cin >> n;
    for(int i = 0; i < n; ++i){
        Matrix m;
        std::cin >> m.r >> m.c;
        List.push_back(m);
    }
    
    std::cout << Mcm(List) << std::endl;
    return 0;
}

C++: /*
    Problem 46
    https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_10_B
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* class */
class Matrix {
public:
    int r, c;
    Matrix () {}
    Matrix (int r, int c) : r(r), c(c) {}
};
/* global variables */
/* function */
/* main */
int main(){
    int n;
    cin >> n;
    vector<Matrix> M(n, Matrix());
    for (int i = 0; i < n; i++) {
        int r, c;
        cin >> r >> c;
        Matrix m = Matrix(r, c);
        M[i] = m;
    }

    // init dp
    vector<vector<int>> dp(n, vector<int>(n));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (i == j) dp[i][j] = 0;
            else dp[i][j] = INF;
    // calc
    // ex) M0 * M1 * M2
    // w=1 , M0*M1, M1*M2
    // w=2 , M0*M1*M2 = min{M0*(M1*M2),  (M0*M1)*M2)}
    //   | 0 | 1 | 2
    // 0 | 0 |w=1|w=2
    // 1 |   | 0 |w=1
    // 2 |   |   | 0
    for (int w = 1; w < n; w++) {  // w+1個の連続したMatrixの積, w個の選択肢
        for (int i = 0; i < n-w; i++) {  // start位置 Mi * Mi+1 * ... * Mi+w
            int j = i + w;  // end位置 Mi * Mi+1 * ... * Mj
            for (int k = i; k < j; k++) {
                int cost = M[i].r * M[k].c * M[j].c;
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + cost);
            }
        }
    }

    cout << dp[0][n-1] << '\n';
}