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

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

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

uint64_t solve(const vector<pair<int, int>>& mat) {
  if (mat.size() < 2) {
    return 0ULL;
  }
  auto n = mat.size();
  vector<vector<uint64_t>> cache(n - 1, vector<uint64_t>(n, no_value));
  for (size_t range = 2; range <= n; ++range) {
    for (size_t first = 0; ; ++first) {
      auto last = first + range - 1;
      if (last == n) {
        break;
      }
      for (size_t mid = first; mid != last; ++mid) {
        auto lhs = first == mid ? 0 : cache[first][mid];
        auto rhs = (mid + 1) == last ? 0 : cache[mid + 1][last];
        auto mul = mat[first].first * mat[last].second * mat[mid].second;
        cache[first][last] = min(cache[first][last], lhs + rhs + mul);
      }
    }
  }
  return cache[0][n - 1];
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> mat;
  for (int i = 0; i < n; ++i) {
    int row, col;
    cin >> row >> col;
    mat.push_back(make_pair(row, col));
  }
  uint64_t result = solve(mat);
  cout << result << endl;
}

C++: #pragma GCC optimize("Ofast")
#include<stdio.h>
#include<algorithm>
using namespace std;
int r[105],c[105],dp[105][105]={0};
int main(){
	int i,j,k,n,len;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&r[i],&c[i]);
	for(i=0;i<n-1;i++)
		dp[i][i+1]=r[i]*c[i]*c[i+1];
	for(len=3;len<=n;len++){
		for(i=0;i<n-len+1;i++){
			j=i+len-1;
			dp[i][j]=2147483647;
			for(k=i;k<j;k++){
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j]);
			}
		}
	}
	printf("%d\n",dp[0][n-1]);
}

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

int solve(int n, vector<int> & dims){
    int dp[100][101] = {{0}};
    for (int span = 2; span != n + 1; ++span){
        for (int first = 0; first + span != n + 1; ++first){
            for (int last = first + span; last != n + 1; ++last){
                int record = 2000000000;
                for (int mid = first + 1; mid != last; ++mid){
                    record = min(record, dp[first][mid] + dp[mid][last] + dims[first] * dims[last] * dims[mid]);
                }
                dp[first][last] = record;
            }
        }
    }
    return dp[0][n];
}


int main()
{
    int n = 0;
    cin >> n;
    vector<int> dims(n + 1);
    int r = 0;
    int c = 0;
    for (int i = 0; i != n; ++i){
        cin >> r >> c;
        dims[i] = r;
    }
    dims[n] = c;
    cout << solve(n, dims) << endl;
    return 0;
}