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 <cassert>// c
#include <iostream>// io
#include <iomanip>
#include <fstream>
#include <sstream>
#include <vector>// container
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <algorithm>// other
#include <complex>
#include <numeric>
#include <functional>
#include <random>
#include <regex>
using namespace std;

typedef long long ll;typedef unsigned long long ull;typedef long double ld;

#define ALL(c) c.begin(),c.end()
template<class T> bool IN(T l,T v,T r){return l<=v && v <r;}
template<class T> void UNIQUE(T v){v.erase(unique(ALL(v)),v.end());}
//debug
#define DUMP(x) cerr << #x <<" = " << (x)
#define LINE() cerr<< " (L" << __LINE__ << ")"

struct range{
	struct Iter{
		int v,step;
		Iter& operator++(){v+=step;return *this;}
		bool operator!=(Iter& itr){return v<itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	range(int i, int n,int step):i({i,step}), n({n,step}){}
	range(int i, int n):range(i,n,1){}
	range(int n):range(0,n){}
	Iter& begin(){return i;}
	Iter& end(){return n;}
};
struct rrange{
	struct Iter{
		int v,step;
		Iter& operator++(){v-=step;return *this;}
		bool operator!=(Iter& itr){return v>itr.v;}
		int& operator*(){return v;}
	};
	Iter i, n;
	rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){}
	rrange(int i, int n):rrange(i,n,1){}
	rrange(int n) :rrange(0,n){}
	Iter& begin(){return n;}
	Iter& end(){return i;}
};

//input
template<typename T1,typename T2> istream& operator >> (istream& is,pair<T1,T2>& p){return is>>p.first>>p.second;}
template<typename T1> istream& operator >> (istream& is,tuple<T1>& t){return is >> get<0>(t);}
template<typename T1,typename T2> istream& operator >> (istream& is,tuple<T1,T2>& t){return is >> get<0>(t) >> get<1>(t);}
template<typename T1,typename T2,typename T3> istream& operator >> (istream& is,tuple<T1,T2,T3>& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> istream& operator >> (istream& is,tuple<T1,T2,T3,T4>& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);}
template<typename T> istream& operator >> (istream& is,vector<T>& as){for(int i:range(as.size()))is >>as[i];return is;}
//output
template<typename T> ostream& operator << (ostream& os, const set<T>& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os<<a;}return os;}
template<typename T1,typename T2> ostream& operator << (ostream& os, const pair<T1,T2>& p){return os<<p.first<<" "<<p.second;}
template<typename K,typename V> ostream& operator << (ostream& os, const map<K,V>& m){bool isF=true;for(auto& p:m){if(!isF)os<<endl;os<<p;isF=false;}return os;}
template<typename T1> ostream& operator << (ostream& os, const tuple<T1>& t){return os << get<0>(t);}
template<typename T1,typename T2> ostream& operator << (ostream& os, const tuple<T1,T2>& t){return os << get<0>(t)<<" "<<get<1>(t);}
template<typename T1,typename T2,typename T3> ostream& operator << (ostream& os, const tuple<T1,T2,T3>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t);}
template<typename T1,typename T2,typename T3,typename T4> ostream& operator << (ostream& os, const tuple<T1,T2,T3,T4>& t){return os << get<0>(t)<<" "<<get<1>(t)<<" "<<get<2>(t)<<" "<<get<3>(t);}
template<typename T> ostream& operator << (ostream& os, const vector<T>& as){for(int i:range(as.size())){if(i!=0)os<<" "; os<<as[i];}return os;}
template<typename T> ostream& operator << (ostream& os, const vector<vector<T>>& as){for(int i:range(as.size())){if(i!=0)os<<endl; os<<as[i];}return os;}

// values
template<typename T> inline T INF(){assert(false);};
template<> inline int INF<int>(){return 1<<28;};
template<> inline ll INF<ll>(){return 1LL<<58;};
template<> inline double INF<double>(){return 1e16;};
template<> inline long double INF<long double>(){return 1e16;};

template<class T> inline T EPS(){assert(false);};
template<> inline int EPS<int>(){return 1;};
template<> inline ll EPS<ll>(){return 1LL;};
template<> inline double EPS<double>(){return 1e-8;};
template<> inline long double EPS<long double>(){return 1e-8;};

// min{2^r | n < 2^r}
template<typename T> T upper_pow2(T n){ T res=1;while(res<n)res<<=1;return res;}
// max{d | 2^d  <= n}
template<typename T> T msb(T n){ int d=63;while((1LL<<d)>n)d--;return d;}

template<typename T,typename U> T pmod(T v,U M){return (v%M+M)%M;}

class Main{
	public:
	void run(){
		int N;cin >> N;
		vector<int> rs(N),cs(N);
		for(int i:range(N))cin >> rs[i] >> cs[i];

		vector<vector<ll>> dp(N,vector<ll>(N+1,INF<ll>()));
		for(int l:range(N)) dp[l][l+1] = 0;

		for(int d:range(2,N+1))for(int l:range(N + 1 -d)){
			int r = l+d;
			for(int m:range(l+1,r)){
				dp[l][r] = min(dp[l][r],dp[l][m] + rs[l] * rs[m] * cs[r-1] + dp[m][r]);
			}
		}
		cout << dp[0][N] <<endl;
	}
};

int main(){
	cout <<fixed<<setprecision(20);
	cin.tie(0);
	ios::sync_with_stdio(false);
	Main().run();
	return 0;
}

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

using namespace std;

static const int N = 100;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
int main(void){
	int n,p[N+1],m[N+1][N+1];
	cin>>n;

	loop(i,1,n+1){
		cin>>p[i-1]>>p[i];
	}

	loop(i,1,n+2)m[i][i] = 0;

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

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

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