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
	} else if len(matrices) == 2 {
		return calcMultiplication(matrices[0], matrices[1])
	}

	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
		}
	}
	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;
using ll = long long;
using ull = unsigned long long;

#define REP(i,n) for(long long i = 0; i < (n); i++)
#define FOR(i, m, n) for(long long i = (m);i < (n); ++i)
#define ALL(obj) (obj).begin(),(obj).end()
#define SPEED cin.tie(0);ios::sync_with_stdio(false);

template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQR = priority_queue<T,vector<T>,greater<T>>;

constexpr long long MOD = (long long)1e9 + 7;
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433;

template <class T> vector<T> make_v(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto make_v(size_t N,T... t){return vector<decltype(make_v(t...))>(N,make_v(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
template <template <class tmp>  class T, class U> ostream &operator<<(ostream &o, const T<U> &obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr)o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

vector<vector<ll>> dp;
vector<ll> L,R;

ll rec(int l,int r) {
    if(dp[l][r]!=-1) return dp[l][r];

    if(l==r) return dp[l][r] = 0;

    ll sum = HIGHINF;
    for(int i = l; i+1 <= r; ++i) {
    
        ll xl = rec(l,i);
        ll xr = rec(i+1,r);    
        chmin(sum,L[l]*R[i]*R[r]+xl+xr);
    }
    return dp[l][r] = sum;
}

int main() {
    int N; cin >> N;
    dp.resize(N,vector<ll>(N,-1));
    L.resize(N);
    R.resize(N);

    for(int i = 0; i < N; ++i) cin >> L[i] >> R[i];
    cout << rec(0,N-1) << endl;

    return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int m[128], costs[128][128];
int calc(int s, int e){
	if(s==e) return 0;
	if(costs[s][e] == INT_MAX){
		if(e-s==1) return costs[s][e] = m[s-1] * m[s] * m[e];
		for(int i = s; i < e; i++){
			costs[s][e] = min(costs[s][e], calc(s, i) + calc(i+1, e) + m[s-1] * m[i] * m[e]);
		}
	}
	return costs[s][e];
}

int main() {
	int n;
	scanf("%d", &n);
	
	for(int i = 0; i < n; i++){
		scanf("%d %d", &m[i], &m[i+1]);
	}
	
	for(int i = 0; i <= n; i++){
		for(int j = 0; j <= n; j++){
			costs[i][j] = INT_MAX;
		}
	}
	
	printf("%d\n", calc(1, n));
	
	return 0;
}

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 dfs(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,dfs(l,i)+dfs(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<<dfs(0,n-1)<<endl;
}

C++: #include <cstdio>

struct Matrix
{
  int row;
  int column;
  int multiple_time;

  Matrix():
    row(0), column(0), multiple_time(0)
  {}

  Matrix(int r, int c, int m):
    row(r), column(c), multiple_time(m)
  {}
};

Matrix multiple(Matrix mat1, Matrix mat2)
{
  return Matrix(mat1.row,
                mat2.column,
                mat1.multiple_time + mat2.multiple_time + mat1.row * mat1.column * mat2.column);
}

Matrix matrices[100];
Matrix multiplication[100][100];

Matrix minimum_multiple_time(int i, int j)
{
  if (i == j)
    return matrices[i];

  Matrix min = multiple(multiplication[i][i], multiplication[i + 1][j]);

  for (int k = 1; k < j - i; ++k)
  {
    Matrix cur_matrix = multiple(multiplication[i][i + k], multiplication[i + k + 1][j]);
    if (min.multiple_time > cur_matrix.multiple_time)
      min = cur_matrix;
  }

  return min;
}

int main()
{
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; ++i)
    scanf("%d%d", &matrices[i].row, &matrices[i].column);

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; i + j < n; ++j)
    {
      multiplication[j][i + j] = minimum_multiple_time(j, i + j);
    }
  }

  printf("%d\n", multiplication[0][n - 1].multiple_time);

  return 0;
}