Go: package main

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

var cache [100][100]int

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	num, _ := strconv.Atoi(sc.Text())
	tables := []table{}
	for i := 0; i < num; i++ {
		sc.Scan()
		num1, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		num2, _ := strconv.Atoi(sc.Text())
		t := table{num1, num2}
		tables = append(tables, t)
	}

	fmt.Println(minimum(0, len(tables)-1, tables))
}

type table struct {
	v int
	h int
}

func minimum(i int, j int, t []table) int {
	if i == j {
		return 0
	}

	// fmt.Printf("i:%v j:%v\n", i, j)
	if cache[i][j] != 0 {
		return cache[i][j]
	}

	min := math.MaxInt64
	for k := i; k < j; k++ {
		p := t[i].v
		n := t[k].h
		q := t[j].h
		val := minimum(i, k, t) + minimum(k+1, j, t) + p*n*q
		if val < min {
			min = val
		}
	}

	cache[i][j] = min
	return min
}

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';
}

C++: #include <stdio.h>
#include <algorithm>

using namespace std;


int main(){
	int n,minimum,tmp;
	scanf("%d",&n);
	int table[n][n];
	int data[n][2];

	for(int i=0;i<n;i++){
		for(int k=0;k<n;k++)table[i][k] = 0;
		scanf("%d %d",&data[i][0],&data[i][1]);
	}

	for(int i=n-2;i>=0;i--){
		for(int k=0;k<=i;k++){
			if(i==n-2){
				table[n-i-1][k] = data[k][0]*data[k][1]*data[k+1][1];
		}else{
				minimum = 2147483647;
				for(int p=0;p<=n-i-2;p++){
					tmp=(table[p][k]+table[n-i-p-2][k+p+1])+(data[k][0]*data[k+p][1]*data[k+n-i-1][1]);
					minimum = min(minimum,tmp);
				}
				table[n-i-1][k] = minimum;
			}
		}
	}

	printf("%d\n",table[n-1][0]);

    return 0;
}

C++: #include<bits/stdc++.h>
#define N 100
using namespace std;

int main() {
  int n,p[N+1],m[N+1][N+1];
  cin>>n;
  for (int i=1;i<=n;i++) cin>>p[i-1]>>p[i];

  for (int i=1;i<=n;i++) m[i][i]=0;
  for (int l=2;l<=n;l++) {
    for (int i=1;i<=n-l+1;i++) {
      int j=i+l-1;
      m[i][j]=(1<<21);
      for (int 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]);
      }
    }
  }
  cout<<m[1][n]<<endl;
  return 0;
}