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 <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxx=2000000000;

int n;
int p[110],dp[110][110];

int main (){
	cin>>n;
	for(int i=1;i<=n;i++){
		dp[i][i]=0;
		cin>>p[i-1]>>p[i];
	}
	for(int len=2;len<=n;len++){
		for(int i=1;i<=n-len+1;i++){
			int j=i+len-1;
			dp[i][j]=maxx;
			for(int k=i;k<j;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;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INFTY 200000000000
long long dp[110][110];
int seen[110][110];
vector<int>ro(110),co(110);
long long ch(int l,int r){
    long long mn = dp[l][r];
    if(r==l) return dp[l][r] = 0;
    if(dp[l][r]!=INFTY) return dp[l][r];
    for (int k = l; k < r; k++)
    {
        long long sum = ch(l,k)+ch(k+1,r)+ro[l]*co[r]*ro[k+1];
        mn = min(mn,sum);
    }  
    return dp[l][r] = mn;
}
int main(){
    int n;cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> ro[i]>>co[i];
    }
    
    for (int i = 0; i < n;i++)
    {
        for(int j=0;j<n;j++){
            dp[i][j] = INFTY;
        }
        }
    long long ans = ch(0,n-1);
    cout << ans << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const int INF = 1e9;
const int MOD = 1e9+7;
using Graph = vector<vector<ll>>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}

// 0 false, 1 true 
// string to int : -48
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp

ll n;
vector<vector<ll>> dp(n, vector<ll>(n, INF));  //dp[i][j] i to j min cost
vector<pair<ll,ll>> vec(n);

ll calc_mult(pair<ll,ll> a, pair<ll,ll> b){
    return a.first * a.second * b.second;
}

ll calc_sec(ll a, ll b){
    if(dp[a][b] != -1){
        return dp[a][b];
    }
    if(b == a){
        return 0;
    }
    if(b - a == 1){
        dp[a][b] = vec[a].first * vec[a].second * vec[b].second;
        return vec[a].first * vec[a].second * vec[b].second;
        //return 0;
    }


    ll res = INF;
    for(ll i = a; i < b; i++){
        res = min(res, calc_sec(a, i) + calc_sec(i+1, b) + vec[a].first * vec[i].second * vec[b].second);
    }

    dp[a][b] = res;
    return res;
}


int main() {
    std::cout << std::fixed << std::setprecision(15);
    cin >> n;

    vec.resize(n);
    rep(i, n) cin >> vec[i].first >> vec[i].second;

    if(n == 1){
        cout << 0 << endl;
        return 0;
    }

    
    dp.resize(n, vector<ll>(n));
    dp.assign(n, vector<ll>(n, -1));
    rep(i,n){
        dp[i][i] = 0;
    }

    /*
    cout << calc_sec(0, 1) << endl;
    cout << calc_sec(0, 2) << endl;
    cout << calc_sec(0, 5) << endl;
    */
    /*
    rep(i,n){
        rep(j,n){
            cout << dp[i][j] << " ";
        }
        br;
    }
    */

    cout << calc_sec(0, n-1) << endl;







}

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

//using Graph = vector<vector<edge>>;
using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define erep(i,container) for (auto i : container)
#define irep(i, n)        for(int i = n-1; i >= (int)0; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define all(x) (x).begin(),(x).end()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query int qq;std::cin >> qq;rep(qqq,qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
#define case std::cout <<"Case #" <<qqq+1<<": "
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define ssum(a) accumulate(a,0ll) 

typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;
//typedef long long T;
const int INF = 2000000000;
const ll INF64 = 922337203685477580ll;
const ll MOD = 1000000007ll;
const dou pi=3.141592653589793;
int main(){
    int n;
    std::cin >> n;
    std::vector<int> a(n),b(n);
    rep(i,n)std::cin >> a[i]>>b[i];
    vvector(dp,int,n+1,n+1,INF);
    rep(i,n+1)dp[i][i]=0;
    rep(i,n)dp[i][i+1]=0;
    rep(i,n-1){
        dp[i][i+2]=a[i]*b[i]*b[i+1];
     //   std::cout << dp[i][i+2] << std::endl;
    }
    
    rrep(i,3,n+1){
        rep(j,n-i+1){
            //dp[j][j+i]=min(dp[j][j+i-1]+a[j]*a[j+i-1]*b[j+i-1],dp[j+1][j+i]+b[j+i-1]*a[j]*b[j]);
            rrep(ii,j+1,i+j)chmin(dp[j][j+i],dp[j][ii]+dp[ii][j+i]+a[j]*b[j+i-1]*a[ii]);
        }
    }
    std::cout << dp[0][n] << std::endl;
    
}