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 <iostream>
#include <queue>
#include <vector>
using namespace std;
#define MAXN 100

struct Matrix {int r, c; };

int main(){
    int n; cin >> n;
    struct Matrix M[MAXN];
    for(int i=0; i<n; ++i){
        cin >> M[i].r >> M[i].c;
    }
    
    int dp[MAXN+1][MAXN];
    for(int i=0; i<n; ++i){
        dp[1][i] = 0;
    }
    for(int m=2; m<=n; ++m){
        for(int i=0; i<=n-m; ++i){
            priority_queue<int, vector<int>, greater<int> > pq;
            for(int j=1; j<m; ++j){
                //cout << "here with m= " << m << " i = " << i << " j = " << j << endl;
                int x = dp[j][i] + dp[m-j][i+j] + M[i].r*M[i+j].r*M[m+i-1].c;
                pq.push(x);
            }
            dp[m][i] = pq.top();
        }
    }
    cout << dp[n][0] << endl;
}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

int N = 100;

int main(){
    int n, p[N+1], m[N+1][N+1];
    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++){
        for (int i=1;i<=n-l+1;i++){
            int j = i + l - 1;
            m[i][j] = (1 << 21);
            for (int k=i;k<=j-1;k++){
                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;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

#define INFTY (1<<21)
#define MAX_N 100

int m[MAX_N + 1][MAX_N + 1], p[MAX_N + 1];
int n;

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

	for( int l = 2; l <= n; l++ ){
		for( int i = 1; i <= n - l + 1; i++ ){
			int j = i + l - 1;
			m[i][j] = INFTY;
			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]);
			}
		}
	}
}

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

	matrixChainMultiplication();

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

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int main() {

	int n, p[101], m[101][101];

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

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

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

#define MAX ((int)110)
#define endl '\n'

using namespace std;

typedef long long ll;

int n, m[ MAX ];
ll DP[ MAX ][ MAX ];

ll solve(){
	int j;
	ll cost;
	for(int len = 2 ; len <= n ; len++){
		for(int i = 1 ; i <= n - len + 1 ; i++){
			j = i + len - 1;
			DP[ i ][ j ] = LONG_LONG_MAX;
			for(int k = i ; k < j ; k++){
				cost = DP[ i ][ k ] + DP[ k + 1 ][ j ] + m[ i - 1 ] * m[ k ] * m[ j ];
				DP[ i ][ j ] = min(DP[ i ][ j ], cost);
			}
		}
	}
	return DP[ 1 ][ n ];
}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m[ 0 ];
	for(int i = 1 ; i < n ; i++)
		cin >> m[ i ] >> m[ i + 1 ];
	cin >> m[ n ];
	cout << solve() << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

int N, M, K, Q, W, H, R, C;
int dp[200][200];

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

    cin >> N;
    vector<int> a(N), b(N);
    REP(i, N) cin >> a.at(i) >> b.at(i);

    REP(i, N + 1) {
        REP(j, N + 1) {
            dp[i][j] = INF;
            if (i + 1 == j) dp[i][j] = 0;
        }
    }

    FOR(len, 1, N) {
        REP(l, N) {
            int r = l + len + 1;
            if (r <= N) {
                FOR(h, l + 1, r) { chmin(dp[l][r], dp[l][h] + dp[h][r] + a[l] * b[h - 1] * b[r - 1]); }
            }
        }
    }

    cout << dp[0][N] << endl;

    return 0;
}