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<algorithm>

#define INF 1<<28

using namespace std;

int n, m[100][100], M[100][2];

int Multi(int a, int b) {
	if (m[a][b] != INF) return m[a][b];
	else if (b - a == 1) m[a][b] = M[a][0] * M[a][1] * M[b][1];
	else {
		for (int i = 1; i < b - a - 1; i++) {
			m[a][b] = min(m[a][b], Multi(a, a + i) + Multi(a + i + 1, b) + M[a][0] * M[a + i][1] * M[b][1]);
		}
		m[a][b] = min(m[a][b], Multi(a, b - 1) + M[a][0] * M[b][0] * M[b][1]);
		m[a][b] = min(m[a][b], Multi(a + 1, b) + M[a][0] * M[a][1] * M[b][1]);
	}
	return m[a][b];
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) cin >> M[i][0] >> M[i][1];
	for (int i = 0; i < n - 1; i++) {
		for (int j = i + 1; j < n; j++) m[i][j] = INF;
	}
	cout << Multi(0, n - 1) << endl;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

using ll = long long;

#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)

#define input(...) __VA_ARGS__; in(__VA_ARGS__)

template<class T>
void print(vector<T> a) {
  cout << "[ ";
  REP(i, a.size()) cout << a[i] << " ";
  cout << "]" << endl;
}

void print() {
  std::cout << std::endl;
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  std::cout << head << " ";
  print(std::forward<Tail>(tail)...);
}

void in() { }

template <class Head, class... Tail>
void in(Head&& head, Tail&&... tail) {
  cin >> head;
  in(std::forward<Tail>(tail)...);
}

ll n;
vector<ll> m;
vector<vector<ll>> table;

#define DP table[left][right]
ll dfs(ll left=0, ll right=n + 1) {
  if (DP != -1) return DP;

  if (right - left <= 2) return DP = 0;
  ll minv = 1ll << 60;
  FOR(c, left + 1, right - 2) {
    minv = min(minv, dfs(left, c + 1) + dfs(c, right) + m[left] * m[c] * m[right - 1]);
  }
  return DP = minv;
}

int main() {
  cin >> n;
  m = vector<ll>(n + 1);
  REP(i, n) {
    cin >> m[i] >> m[i + 1];
  }

  table = vector<vector<ll>>(n + 2, vector<ll>(n + 2, -1));

  cout << dfs() << endl;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); ++i)
#define repr(i, a, b) for (int i=a; i<(b); ++i)
#define reprev(i, n) for (int i=n-1; i>=0; --i)
#define reprrev(i, a, b) for (int i=b-1; i>=(a); --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    cout << fixed << setprecision(10);
    
    int n; cin >> n;
    vector<P> mt(n);
    rep(i, n) {
        cin >> mt[i].first >> mt[i].second;
    }
    // iからjまでの区間で最小の数
    vector<vector<int>> dp(n, vector<int>(n));

    // 区間を小さい方から始める
    repr(i, 2, n+1) {
        // iは区間の大きさ 2 <= i <= nまで
        int wj = i-1;
        rep(sj, n+1-i) {
            // jは区間の始める点, i = 2のとき、jは 0 <= j < n+1 - 2
            int lj = sj + wj;
            // [sj, wj]間を二つに分割して一番最小なコストを探索する
            // 分割点の候補は [sj, K), [K, lj]で、sj+1 < K < lj
            int min_cost = 100000001;
            repr(fk, sj+1, lj+1) {
                // fkは 分割するkの場所
                min_cost = min(min_cost, dp[sj][fk-1] + dp[fk][lj] + mt[sj].first * mt[fk].first * mt[lj].second);
            }
            dp[sj][lj] = min_cost;
        }
    }

    cout << dp[0][n-1] << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    vector<int> r(n), c(n);
    rep(i, n) cin >> r[i] >> c[i];

    const int INF = 1001001001;
    vector<vector<int>> dp(n, vector<int>(n, INF));
    rep(i, n) dp[i][i] = 0;
    for(int w = 1; w < n; ++w) for(int i = 0; i < n-w; ++i){
        int j = i + w;
        for(int k = i; k < j; ++k){
            dp[i][i+w] = min(dp[i][i+w], dp[i][k] + dp[k+1][i+w] + r[i]*c[k]*c[j]);
        }
    }

    // rep(i, n){
    //     rep(j, n) cout << dp[i][j] << " ";
    //     cout << endl;
    // }

    cout << dp[0][n-1] << endl;

    return 0;
}

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