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>
using namespace std;

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

int main() {
	int n, p[1000], m[1000][1000];
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> p[i - 1] >> p[i];
	MatrixChainMultiplication(n, p, m);
	cout << m[1][n] << endl;

	
}

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

using namespace std;

typedef long long ll;

const ll INF = (1LL << 60);

pair<ll, ll> A[110];
ll dp[110][110];

ll matrix_chain(int i, int j){
	if(i == j) return 0;
	if(dp[i][j] != INF) return dp[i][j];
	for(int k = i;k < j; ++k) dp[i][j] = min(dp[i][j], matrix_chain(i, k) 
			+ matrix_chain(k+1, j) + A[i].first*A[k].second*A[j].second);
	return dp[i][j];
}

int main(){

	ios::sync_with_stdio(0);
	cin.tie(0);

	int n;
	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++) cin >> A[i].first >> A[i].second;
	
	cout << matrix_chain(0, n-1);

	return 0;
} // br1

C++: #pragma GCC optimize ("O3")

#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...)  cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif

void d_err() {
    cerr << endl;
}

template <typename H, typename... T>
void d_err(H h, T... t) {
    cerr << h << " ";
    d_err(t...);
}

template <typename T>
void print(T x) {
    cout << x << "\n";
}

#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (int i = (n - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;

ll INF = 1e16;
int inf = 1e9;
ll MOD = 1e9+7;
//
//ll matrix_chain_multiplications(vector<int>& p) {
//    int n = p.size() - 1; // num of matrix
//    vector<vll> dp(n + 1, vll(n + 1, INF));
//
//    // init
//    FOR(i, 1, n + 1) dp[i][i] = 0;
//
//    FOR(l, 2, n + 1) {
//        FOR(i, 1, n - l + 2) {
//            int j = i + l - 1;
//            FOR(k, i, j) {
//                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i - 1] * p[k] * p[j]);
//            }
//        }
//    }
//
//    return dp[1][n];
//}

// 0-indexed
ll matrix_chain_multiplications(vector<int>& p) {
    int n = p.size() - 1; // num of matrix
    vector<vll> dp(n, vll(n, INF)); // dp[l][r] = minimum cost of [l, r], 0 <= l, r < n

    // init
    FOR(i, 0, n) dp[i][i] = 0;

    FOR(l, 2, n + 1) { // l = 2 ~ n
        REP(i, n - l + 1) {
            int j = i + l - 1;
            FOR(k, i, j) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + p[i] * p[k + 1] * p[j + 1]);
            }
        }
    }

    return dp[0][n - 1];
}


int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int n; cin >> n;

    vi p(n + 1);
    REP(i, n) cin >> p[i] >> p[i + 1];

    print(matrix_chain_multiplications(p));
}

C++: #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

struct mat {
    int row = -1;
    int col = -1;
};

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

    int N;
    cin >> N;
    vector<mat> M(N);
    rep(i, N) {
        int a, b;
        cin >> a >> b;
        M[i].row = a, M[i].col = b;
    }

    vector<vector<ll>> dp(N + 5, vector<ll>(N + 5, INF));
    rep(i, N + 5) { dp[i][i] = 0; }

    for (int w = 1; w < N; w++) {
        for (int i = 0; i < N; i++) {
            if (i + w >= N)
                continue;
            int j = i + w;
            for (int k = i; k < j; k++) {
                ll num_pro = M[i].row * M[k].col * M[j].col;
                dp[i][i + w] =
                    min(dp[i][i + w], dp[i][k] + dp[k + 1][i + w] + num_pro);
            }
        }
    }

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

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

int calcDpTable(int i, int j, int ** dpTable, const int *p) {
	int minValue = INT_MAX; 
	for (int k = i; k < j; ++k) {
		minValue = min(dpTable[i][k - i] + dpTable[k + 1][j - k - 1] + p[i] * p[k + 1] * p[j + 1], minValue);
	}
	//dpTable[0][0] = 1;
	return minValue;
}

int minNumOfMultiplications(const int *p, int n) {
	int **dpTable = new int*[n];
	for (int i = 0; i < n; ++i) {
		dpTable[i] = new int[n - i];
		dpTable[i][0] = 0;
	}

	int d = 1;
	for (int to = n - 2; to >= 0; --to) {
		for (int i = 0; i <= to; ++i) {
			int j = i + d;
			dpTable[i][j - i] = calcDpTable(i, j, dpTable, p);
		}
		++d;
	}
	int ret = dpTable[0][n - 1];
	
	for (int i = 0; i < n; ++i) {
		delete[] dpTable[i];
	}
	delete[] dpTable;

	return ret;
}

int main() {
	int n;
	cin >> n;
	int *p = new int[n + 1];
	for (int i = 0; i < n; ++i) {
		cin >> p[i] >> p[i + 1];
	}
	cout << minNumOfMultiplications(p, n) << endl;
}

C++: #include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;
int AskMCM(int *p,int n){
    int cost[n+1][n+1];
    for(int i = 1;i <= n;++i) cost[i][i] =0;
    for(int l = 2;l <= n; l++){
        for(int i = 1;i <= n - l + 1;i++){
            int j = i+l -1;
            int min_cost = INT_MAX;
            for(int k = i;k < j;++k) min_cost = min(min_cost,cost[i][k] + cost[k+1][j] + p[i-1] * p[k] * p[j]);
            cost[i][j] = min_cost;
        }
    }
    return cost[1][n];
}

int main(){
    int n,r,tmp;
    scanf("%d",&n);
    int p[n+1];
    for(int i = 1;i <= n;++i) scanf("%d %d",&p[i-1],&p[i]);
    printf("%d\n",AskMCM(p,n));
}