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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;
typedef pair<int, int> P;
const int INF = 1<<22;

// dp[i][j] := [i , j]??????????????????????????則???????????????????属???即????????属
// |(4*5   (5*3)  3*2)|  2*1
int main()
{
	int n, dp[128][128], r[128];
	P T[128];

	cin >> n;

	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
		 	dp[i][j] = INF;
		}
	}
	/*
	for(int i = 0; i < n; i++)
	{
		dp[i][i] = 0;
	}
	*/
	for(int i = 0; i < n; i++)
	{
		cin >> T[i].first >> T[i].second;
		dp[i][i] = 0;
	}
	/*
	r[0] = T[0].first;
	for(int i = 0; i < n; i++)
	{
		r[i+1] = T[i].second;
	}
	*/
	/*
	for(int i = 0; i <= n; i++)
	{
		cout << r[i] << endl
	}
	*/
	/*
	for(int l = 1; l < n; l++)
	{
		for(int i = 0; i < n-l; i++)
		{ 
			int j = i + l;
			for(int k = 1; k <= l; k++)
			{
				dp[i][j] = std::min(dp[i][j], dp[i][i + k - 1] + dp[i + k][j] + T[i].first*T[i + k].first*T[j].second); //r[j]*r[k]*r[i+j+1]);
			}
		}
	}
*/
	for(int i = 1; i < n; i++)
	{
		for(int j = 0; j < n-i; j++)
		{ 
			for(int k = 1; k <= i; k++)
			{
				dp[i][j] = std::min(dp[i][j], dp[k-1][j] + dp[i-k][j+k] + T[j].first*T[j + k].first*T[j + i].second); //r[j]*r[k]*r[i+j+1]);
				printf("%3d : %3d : %3d => %3d\n", i, j, k, dp[i][j]);
				//puts("run");
			}
		}
	}

	/*
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			printf("%10d ",dp[i][j]);
		}
		puts("");
	}
	*/

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

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
const int N = 101;

int n;
long m[N][N], p[N+2];
int j;

int matrixChainOrder(){
  for(int i=0; i<n; i++){ m[i][i] = 0; }
  for(int l=2; l<=n; l++){
    for(int i=1; i+l-1<=n; i++){
      j = i+l-1;
      m[i][j] = 100000000;
      for(int k=i; k<j-1; k++){
        m[i][j] = std::min(m[i][j], m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]);
      }
    }
  }
  return m[1][n];
}

int main(){
  std::cin >> n; //number of matrix

  for(int i=1; i<=n; i++){
    std::cin >> p[i-1] >> p[i];
  }
  std::cout << matrixChainOrder() << std::endl;
  return 0;
}

C++: #include <cassert>
#include <climits>
#include <iostream>
#include <vector>

class Matrix {
  public:
    long long x;
    long long y;
    long long cost;
};

int main()
{
  int n;
  std::cin >> n;
  std::vector<Matrix> matrix(n);
  for (int i = 0; i < (int)matrix.size(); i++) {
    std::cin >> matrix.at(i).x >> matrix.at(i).y;
  }
  std::vector<std::vector<Matrix>> memo(n+1, std::vector<Matrix>(n));
  for (int i = 0; i < (int)matrix.size(); i++) {
    memo.at(1).at(i).x = matrix.at(i).x;
    memo.at(1).at(i).y = matrix.at(i).y;
  }
  for (int block = 2; block <= (int)matrix.size(); block++) {
    for (int i = 0; i + block <= (int)matrix.size(); i++) {
      long long result = LLONG_MAX;
      memo.at(block).at(i).x = matrix.at(i).x;
      memo.at(block).at(i).y = matrix.at(i+block-1).y;
      for (int part = i+1; part < i + block; part++) {
        long long tmp = 0;
        assert(memo.at(part-i).at(i).y == memo.at(block-part+i).at(part).x);
        tmp += memo.at(part-i).at(i).cost + memo.at(block-part+i).at(part).cost;
        tmp += memo.at(part-i).at(i).x * memo.at(part-i).at(i).y * memo.at(block-part+i).at(part).y;
        result = std::min(result, tmp);
      }
      memo.at(block).at(i).cost = result;
    }
  }
  if (matrix.size() == 1) {
    std::cout << "0" << std::endl;
  } else {
    std::cout << memo.at(matrix.size()).at(0).cost << std::endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int dp[100][100];
int size[100][2];

int main() {
    int n;
    cin >> n;
    REP(i, 100) REP(j, 100)
        dp[i][j] = INF;
    REP(i, n)
        cin >> size[i][0] >> size[i][1];
    REP(i, n) for(int j = 0; j < n - i; j++){
        if(i == 0){
            dp[i][j] = 0;
            continue;
        }
        if(i == 1){
            dp[i][j] = size[j][0] * size[j][1] * size[j+1][1];
            continue;
        }
        REP(k, i){
            dp[i][j] = min(dp[i][j], dp[k][j] + dp[i-1-k][j+k+1] + size[j][0] * size[j+k][1] * size[j+i][1]);
        }
    }
    cout << dp[n-1][0] << endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cstdint>

using namespace std;

int main() {
    int32_t n;
    cin >> n;

    vector<int32_t> r(n), c(n);
    for (int32_t i = 0; i < n; i++) {
        cin >> r[i] >> c[i];
    }

    vector<int32_t> table(n*n);
    for (int32_t i = 0; i < n; i++) {
        table[i*n+i] = 0;
    }
    for (int32_t i = n-1; i >= 0; i--) {
        for (int32_t j = i+1; j < n; j++) {
            table[i*n+j] = INT32_MAX;
            for (int32_t k = i; k < j; k++) {
                table[i*n+j] = min(table[i*n+j], table[i*n+k] + table[(k+1)*n+j] + r[i] * c[k] * c[j]);
            }
        }
    }

    cout << table[n-1] << endl;

    return 0;
}

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

#define rep(i, n) for (int i=0; i<(n); ++i)
using ll = long long;
using P = pair<int, int>;

const int MAX = 103;
const int INF = 1 << 21;
int dp[MAX][MAX];
int p[MAX];

void solve(int n) {
  for (int i=1; i<=n; i++) {
    dp[i][i] = 0;
  }

  for (int l=2; l<=n; l++) {
    for (int i=1; i<=(n-l+1); i++) {
      int j = i + l - 1;
      dp[i][j] = INF;
      for (int k=i; k<=(j-1); k++) {
	dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i-1] * p[k] * p[j]);
      }
    }
  }
}

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

  solve(n);
  cout << dp[1][n] << endl;
  return 0;
}