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 <cstdio>
#include <algorithm>
using namespace std;

const int INF = 1000000000;

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

  int m[n + 1][n + 1], p[n + 1];
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      m[i][j] = INF;

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

  scanf("%d%d", &v1, &v2);
  p[0] = v1;
  p[1] = v2;
  for (int i = 1; i < n; i++)
  {
    scanf("%d%d", &v1, &v2);
    p[i + 1] = v2;
  }

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

  printf("%d\n", m[1][n]);
  return 0;
}

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

#define fi first
#define se second
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb(s) push_back(s)
#define mp(a,b) make_pair(a,b)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define maxch(x,y) x=max(x,y)
#define minch(x,y) x=min(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF 1e15

#define MAX_N 1000

int n;
ll p[111],m[111][111];

int main(){
	cin.sync_with_stdio(false);
	cin>>n;
	repl(i,1,n+1){
		cin>>p[i-1]>>p[i];
	}
	repl(l,2,n+1)repl(i,1,n-l+2){
		int j=i+l-1;
		m[i][j]=INF;
		repl(k,i,j)minch(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
	}
	cout<<m[1][n]<<endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1e7;
const ll MOD = 1e9 + 7;
const int MAX = 101;

int n; // 行列の数
int m[MAX][MAX]; // m[i][j] := 行列iと行列jの最小の演算数
int p[MAX]; // p[i] := 行列iの行数(縦の長さ)

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> p[i - 1] >> p[i];
    }

    for (int i = 1; i <= n; ++i) {
        m[i][i] = 0;
    }
    for (int l = 2; l <= n; ++l) { // 行列のlengthは2~n
        for (int i = 1; i <= n-l+1; ++i) { // iからn-l+1まで
            int j = i + l - 1;
            m[i][j] = (1 << 21);
            for (int k = i; k < j; ++k) { // m[i][j]を求める
                m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
            }
        }
    }

    cout << m[1][n] << endl;

    return 0;
}

C++: //============================================================================
// Name        : MatrixChainMultiplication.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <map>
using namespace std;

struct Matrix{
	int row;
	int col;
};

class MatrixList{
public:
	vector<Matrix> matrices;
	map<string, int> minProcess;
	map<string, int>::iterator p;
	MatrixList():matrices(), minProcess(){}

	void insert(const Matrix& mat){
		matrices.push_back(mat);
	}

	int calcMinProcess(int i, int j){
		if(not(i>=0 and i<j and j<=matrices.size())){
			cout << "calcMinProcess: i and j are invalid\n";
			return -1;
		}

		string key = to_string(i) + ":" + to_string(j);

		p = minProcess.find(key);
		if(p != minProcess.end()){
			return minProcess[key];
		}

		if(j == i+1){
			return 0;
		}

		int min = 100000000;

		for(int k=i+1; k<j; k++){
			int tmp = calcMinProcess(i, k) + calcMinProcess(k, j) + matrices.at(i).row * matrices.at(k-1).col * matrices.at(j-1).col;
			if(tmp < min){
				min = tmp;
			}
		}

		minProcess[key] = min;
		return min;
	}

	void writeMatList(){
		for(int i=0; i<matrices.size(); i++){
			cout << matrices.at(i).row << " " << matrices.at(i).col << "\n";
		}
	}
};

int main() {
	int n, row, col;
	cin >> n;
	MatrixList matList;

	for(int i=0; i<n; i++){
		cin >> row >> col;
		Matrix mat = {row, col};
		matList.insert(mat);
	}

	//matList.writeMatList();
	cout << matList.calcMinProcess(0, matList.matrices.size()) << "\n";

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int n;
int r[100], c[100];
int dp[100][100];
const int INF = 1000000000;

int main() {
  cin >> n;
  rep(i, n) {
    cin >> r[i] >> c[i]; 
  }
  rep(i, 100) {
    rep(j, 100) {
      if(i == j) dp[i][j] = 0;
      else dp[i][j] = INF;
    }
  }

  for(int w = 1; w < n; w++) {
    for(int i = 0; i + w < n; i++) {
      int j = i + w;
      for(int k = 0; k < j; k++) {
        int num_prod = r[i] * c[k] * c[j];
        dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + num_prod);
      }
    }
  }

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

C++: #include <iostream>
#include <algorithm>
const int MAX_N = 100;
const int INF = 999999999;
using namespace std;

int dp[MAX_N+1][MAX_N+1];
int p[MAX_N+1];

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

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

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

    return 0;
}