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 <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
#include <iomanip>
using namespace std;

#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<long double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }
#define INF 1LL<<60

int main() {
    int N;
    cin >> N;
    vector<P> dim(N);
    REP(i, N) cin >> dim[i].first >> dim[i].second;

    vector< vector<int> > dp(N, vector<int>(N, 1e9));
    vector< vector<P> > mat_size(N, vector<P>(N));
    REP(i, N)
        FOR(j, i, N-1) mat_size[i][j] = make_pair(dim[i].first, dim[j].second);
    REP(i, N) dp[i][i] = 0;
    FOR(l, 1, N - 1) {
        REP(i, N - l) {
            FOR(j, 0, l-1)
                dp[i][i + l] = min(dp[i][i + l],  mat_size[i][i+j].second * mat_size[i][i+j].first * mat_size[i+j+1][i+l].second  + dp[i][i+j] + dp[i+j+1][i+l]);
        }
    }

    // REP(i, N) {
    //     REP(j, N) cerr << dp[i][j] << " ";
    //     cerr << endl;
    // }

    cout << dp[0][N-1] << endl;
    return 0;
}

C++: #include <iostream>
using namespace std;

const int MAX = 105;

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

  for (int i = 0; i <= n; i++) {
    m[i][i] = 0;
  }

  for (int i = 2; i <=n; i++) {
    for (int j = 1; j <= n - i + 1; j++) {
      int k = j + i - 1;
      m[j][k] = (1 << 21);
      for (int l = j; l < k; l++) {
        long long tmp = m[j][l] + m[l+1][k] + p[j-1] * p[l] * p[k];
        m[j][k] = m[j][k] > tmp ? tmp : m[j][k];
      }
    }
  }
  cout << m[1][n] << endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

int main(){
	int n;
	cin >> n;
	int i, t, A[n+1];
	cin >> A[0] >> A[1];
	for(i = 2; i <= n; i++){
		cin >> t >> A[i];
	}

	//dp[l][r] -> min(A[l]*...*A[r])
	int dp[n][n+1]{}, dplr, l, r;
	for(int i = 1; i <= n; i++){
		for(int j = 0; j < n-i; j++){
			l = j;
			r = j+i;
			dplr = INT_MAX;
			for(int k = l; k < r; k++){
				dplr = min(dplr, dp[l][k] + dp[k+1][r] + A[l]*A[k+1]*A[r+1]);
			}
			dp[l][r] = dplr;
			//printf("[%d, %d]=%d ", l, r, dplr);
		}
		//printf("\n");
	}

	printf("%d\n", dp[0][n]);
}

C++: /**
 * 	matrix chain multiplication
 * 	f(n,m) = multiply Mn*M(n+1)*...*M(m-1)*M(m)
 * 	f(n,n) = 1
 * 	r(n)=c(n-1)
 * 	f(n,n+1) = C(n) * R(n) * C(n+1) = c(n) * c(n-1) * c(n+1)
 * 	f(n,k,m) = f(n,k)*f(k+1,m)*C(n)*r(n)*c(k)
 * 	f(n,m) = min(f(n,k,m)
 */

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int r[45] = {0};
int a[100][100] = {0};

int f(int n, int m);
int f(int n, int k, int m);


int f(int n, int k, int m)
{
	return f(n,k) + f(k+1,m) + (r[n] * r[k+1] * r[m+1]);
}

int f(int n, int m)
{
	if (n==m) return 0;
	int & ra = a[n][m];
	if (ra) return ra;
	ra = INT_MAX;
	for (int k=n;k<m;k++)
	{
		int ans = f(n,k,m);
		if (ans < ra) ra = ans;
	}
	return ra;
}

int main()
{
	int rr = 0;
	int cc = 0;
	int n = 0;
	cin >> n;
	for (int i=0;i<n;i++)
	{
		cin >> rr >> cc;
		r[i]=rr;
		r[i+1]=cc;
	}
	for (int i=0;i<n-1;i++)
	{
		a[i][i+1] = r[i] * r[i+1] * r[i+2];
	}
	int ans = f(0,n-1);
	cout << ans << endl;
}

C++: #include <iostream>
#include<math.h>
using namespace std;
int m[1000][1000];
int p[10000];
#define MAX 10000000
int n;
int x(){
    int q;
    for(int i=0;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]=MAX;
            for(int k=i;k<=j-1;k++){
                q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
                if(q<m[i][j]){
                    m[i][j]=q;
                }
            }
        }
    }return m[1][n];
}
int main(){
   
    cin >> n;
    for(int i=0;i<n;i++){
        cin >> p[i]>>p[i+1];
    }
    cout << x()<<endl;
}

C++: #include <iostream>
using namespace std;

int main(){
	int n, x, y;
	cin >> n;
	int a[100][2], c[100][100] = {0};
	for ( int i = 0; i < n; i++ ) 
		cin >> a[i][0] >> a[i][1];
	for (int i = 2; i <= n; i++){
		for (int j = 0; j <= n - i; j++){
			for (int k = 1; k < i; k++){
				y = c[j][k] + c[j+k][i-k] + a[j][0]*a[j+k][0]*a[j+i-1][1];
				x = (x == 0 || x > y) ? y : x;
			}
			c[j][i] = x;
      x = 0;
		}
	}
	cout << c[0][n] << endl;
	return 0;
}