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 (r Range) length() int {
	return r.end - r.start + 1
}

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[r.start].r, matrices[r.end].c}, v
	}
	if r.length() == 0 {
		panic("must not be empty")
	} else if r.length() == 1 {
		return matrices[r.start], 0
	}

	var minCount int = math.MaxInt32

	for i := 0; i < r.length()-1; i++ {
		formerRange := Range{r.start, r.start + i}
		latterRange := Range{r.start + i + 1, r.end}
		matF, countF := calcMinMatrixChainMultiplication(matrices, formerRange, memo)
		matL, countL := calcMinMatrixChainMultiplication(matrices, latterRange, memo)
		_, countFL := calcMultiplication(matF, matL)
		count := countF + countL + countFL
		if count < minCount {
			minCount = count
		}
	}
	memo[r] = minCount
	return Matrix{matrices[r.start].r, matrices[r.end].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())
}

C++: #include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
#include<unordered_map>
#include<fstream>
#include<list>
#include<typeinfo>
#include<functional>
#include<complex>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
const ll E=1e18+7;
const ll MOD=1000000007;



int main(){
    ll n;
    cin>>n;
    vector<vector<ll>> dp(n+1,vector<ll>(n+1,E));
    vector<pll> A(n);
    for(int i=0;i<n;i++){
        dp[1][i]=0;
        ll r,c;
        cin>>r>>c;
        A[i]={r,c};
    }
    for(int i=2;i<=n;i++){
        for(int t=0;i+t<=n;t++){
            for(int h=0;h+1<i;h++){
                //cout<<i<<" "<<t<<" "<<h<<endl;
                dp[i][t]=min(dp[i][t],dp[h+1][t]+dp[i-h-1][t+h+1]+A[t].F*A[t+h].S*A[t+i-1].S);
            }
        }
    }
    cout<<dp[n][0]<<endl;
    
    
    return 0;
}

C++: // 連鎖行列積 問題
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

#define MAX_N 100
#define ELE_NUM 2
// 無効な値の定義
const int INV = -1;

const int colNo = 0;
const int rowNo = 1;
const int IntMAX = 	2147483647;

int ChainedMatrixtMultiplication(int n, int m, int matrixData[][ELE_NUM]);

int main(void)
{

    int n, count;
    int matrix_data[MAX_N + 1][ELE_NUM];

    cin >> n;

    for(int i = 1; i <= n; i++)
    {
        // 行
        cin >> matrix_data[i][colNo];
        // 列
        cin >> matrix_data[i][rowNo];
    }

    count = ChainedMatrixtMultiplication(n, ELE_NUM, matrix_data);
    printf("%d\n", count);

    return 0;
}

// 連鎖行列積の最小の乗算回数を演算する関数
// n : 積を求める行列の数
// m : 行列の情報 : 0 - 行の情報, 1 - 列の情報
// matrixData[][] : 各行列の行、列情報を保持する配列
int ChainedMatrixtMultiplication(int n, int m, int matrixData[][ELE_NUM])
{

    // 第1要素 : 演算開始のNo., 第2要素 : 演算終了のNo.
    int dp[MAX_N + 1][MAX_N + 1];
    int calcResult = 0;

    // 配列の初期化
    for(int i = 0; i <= n; i++)
    {
        for(int j = 0; j <= n; j++)
        {
            dp[i][j] = INV;

            if( i == j )
            {
                // 同じ要素同士の積は演算することが不可能
                dp[i][j] = 0;
            }
        }
    }

    // 行列の演算回数の計算
    // i : 行列の演算をする先頭の要素No.
    // j : 行列の演算をする最後の要素No.
    // k : i 〜 jまでの範囲を移動する変数
    for(int l = 2; l <= n; l++)
    {
        for(int i = 1; i <= n - l + 1; i++)
        {
            int j = i + l - 1;

            dp[i][j] = IntMAX;

            for(int k = i; k <= j - 1; k++)
            {
                calcResult = dp[i][k] + dp[k + 1][j]
                            + ( matrixData[i][colNo] * matrixData[k][rowNo] * matrixData[j][rowNo]);

                dp[i][j] = min( dp[i][j], calcResult );
            }
        }
    }

    return dp[1][n];
}

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

#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int INF=1e9;
int n;
int r[102],c[102];
int p[102];

int dp[102][102];
// [i,j)
int dfs(int i,int j) {
    //cout<<i<<", "<<j<<endl;
    if(dp[i][j]!=-1) return dp[i][j];
    if(j-i==1) return r[i]*c[i]*c[j-1];
    if(i==j) return 0;
    int ret=INF;
    for(int k=i+1;k<j;k++) {
        //ret=min(ret,dfs(i,k)+dfs(k,j)+r[i]*c[k]*c[j-1]);
        ret=min(ret,dfs(i,k)+dfs(k,j)+p[i]*p[k]*p[j]);
    }

    return dp[i][j]=ret;
}

int main() {
    memset(dp,-1,sizeof(dp));
    cin>>n;
    //rep(i,n) cin>>r[i]>>c[i];
    rep(i,n+1) if(i) cin>>p[i-1]>>p[i];
    cout<<dfs(0,n)<<endl;
    return 0;
}

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

using Lint=long long;
const int INF=1e9;

int main()
{
   int N; cin>>N;
   vector<int> row(N),col(N);
   for(int i=0;i<N;i++) cin>>row[i]>>col[i];

   vector<vector<int>> dp(N+1,vector<int>(N+1,INF));
   for(int i=0;i<N;i++) dp[i][i+1]=0;
   for(int len=2;len<=N;len++){
      for(int l=0;l+len<=N;l++){
         int r=l+len;
         for(int m=l+1;m<r;m++) dp[l][r]=min(dp[l][r],dp[l][m]+dp[m][r]+row[l]*row[m]*col[r-1]);
      }
   }
   cout<<dp[0][N]<<endl;
   return 0;
}

C++: #include <stdio.h>

#define MAX 101
#define INF 10000000;


void MatrixChainOrder();
int min(int,int);

int m[MAX][MAX];
int p[MAX];

int n;

int main(){

  int i,j,r,c;

  scanf("%d",&n);

  for(i=0;i<n;i++){
    scanf("%d%d",&r,&c);
    p[i]=r;
    p[i+1]=c;
  }

  MatrixChainOrder();

  printf("%d",m[1][n]);

  return 0;
}


void MatrixChainOrder(){

  int i,j,k,l,q;

  for(i=1;i<=n;i++) m[i][i]=0;

  for(l=2;l<=n;l++){

    for(i=1;n-l+1;i++){
      j=i+l-1;
      m[i][j]=INF;

      for(k=i;k<=j-1;k++){
	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	m[i][j]=min(m[i][j],q);
}
}
}

}

int min(int x,int y){

  if(x<=y) return x;
  else return y;

}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxx=2000000000;

int n;
int p[110],dp[110][110];

int main (){
	cin>>n;
	for(int i=1;i<=n;i++){
		dp[i][i]=0;
		cin>>p[i-1]>>p[i];
	}
	for(int len=2;len<=n;len++){
		for(int i=1;i<=n-len+1;i++){
			int j=i+len-1;
			dp[i][j]=maxx;
			for(int k=i;k<j;k++) dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]);
		}
	}
	cout<<dp[1][n]<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INFTY 200000000000
long long dp[110][110];
int seen[110][110];
vector<int>ro(110),co(110);
long long ch(int l,int r){
    long long mn = dp[l][r];
    if(r==l) return dp[l][r] = 0;
    if(dp[l][r]!=INFTY) return dp[l][r];
    for (int k = l; k < r; k++)
    {
        long long sum = ch(l,k)+ch(k+1,r)+ro[l]*co[r]*ro[k+1];
        mn = min(mn,sum);
    }  
    return dp[l][r] = mn;
}
int main(){
    int n;cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> ro[i]>>co[i];
    }
    
    for (int i = 0; i < n;i++)
    {
        for(int j=0;j<n;j++){
            dp[i][j] = INFTY;
        }
        }
    long long ans = ch(0,n-1);
    cout << ans << endl;

    return 0;
}