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

C++: #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <limits.h>
using namespace std;

int m[101][101]; // m[i][j]: i ?????????j ????????§
int p[101]; // 
int main(){
	int n; scanf("%d", &n);
	for (int i = 0; i < n; ++i){
		int r, c; scanf("%d %d", &r, &c);
		p[i] = r; p[i + 1] = c;
	}
	// initialize m[][]
	for (int i = 0; i <= 100; ++i){
		for (int j = 0; j <= 100; ++j) m[i][j] = INT_MAX;
	}
	for (int i = 0; i <= 100; ++i) m[i][i] = 0;
	//
	for (int l = 2; l <= n; ++l){
		for (int i = 1; i <= n; ++i){
			int j = i + l - 1;
			for (int k = i; k < j; ++k){
				if (m[i][k] == INT_MAX || m[k + 1][j] == INT_MAX) continue;
				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] << '\n';
}

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

int lcs(std::string x, std::string y) {
	int c[101][101];
	int m = x.size();
	int n = y.size();
	int maxl = 0;
	x = ' ' + x;
	y = ' ' + y;
	for (int i = 1; i <= m; i++) {
		c[i][0] = 0;
	}
	for (int i = 1; i <= n; i++) {
		c[0][i] = 0;
	}

	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			if (x[i] == y[j]) {
				c[i][j] = c[i - 1][j - 1] + 1;
			} else {
				c[i][j] = std::max(c[i - 1][j], c[i][j - 1]);
			}
			maxl = std::max(maxl, c[i][j]);
		}
	}
	return maxl;
}


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

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